Computer Science
Predict the output of the following code:
a = 10
y = 5
def myfunc (a) :
y = a
a = 2
print ("y=",y, "a=", a)
print ("a+y", a + y)
return a + y
print ("y=",y, "a=", a)
print (myfunc (a))
print ("y=", y, "a=", a)
Python
Python Functions
3 Likes
Answer
y= 5 a= 10
y= 10 a= 2
a+y 12
12
y= 5 a= 10
Working
a = 10
— This line assigns the value 10 to the global variablea
.y = 5
— This line assigns the value 5 to the global variabley
.def myfunc(a):
— This line defines a function namedmyfunc
that takes a parametera
.y = a
— This line assigns the value of the parametera
(which is 10) to the local variabley
within the function.a = 2
— This line assigns the value 2 to the local variablea
within the function, effectively shadowing the global variablea
.print("y=", y, "a=", a)
— This line prints the values of the local variablesy
anda
within the function.print("a+y", a + y)
— This line prints the sum of the local variablesa
andy
within the function.return a + y
— This line returns the sum of the local variablesa
andy
within the function.print("y=", y, "a=", a)
— This line prints the values of the global variablesy
anda
outside the function.print(myfunc(a))
— This line calls themyfunc
function with the value of the global variablea
(which is 10) as an argument and prints the returned value.print("y=", y, "a=", a)
— This line prints the values of the global variablesy
anda
again.
Answered By
1 Like
Related Questions
What will be the output of the following program?
def display(): print ("Hello",) display() print("bye!")
What is wrong with the following function definition ?
def addEm(x, y, z): return x + y + z print("the answer is", x + y + z)
Find the errors in code given below :
def minus(total, decrement) output = total - decrement print(output) return (output)
Find the errors in the code given below:
define check() N = input ( 'Enter N:'_ I = 3 Answer = 1 + i ** 4/N Return answer