Class - 12 CBSE Computer Science Important Output Questions 2025
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 namedmyfuncthat takes a parametera.y = a— This line assigns the value of the parametera(which is 10) to the local variableywithin the function.a = 2— This line assigns the value 2 to the local variableawithin the function, effectively shadowing the global variablea.print("y=", y, "a=", a)— This line prints the values of the local variablesyandawithin the function.print("a+y", a + y)— This line prints the sum of the local variablesaandywithin the function.return a + y— This line returns the sum of the local variablesaandywithin the function.print("y=", y, "a=", a)— This line prints the values of the global variablesyandaoutside the function.print(myfunc(a))— This line calls themyfuncfunction 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 variablesyandaagain.
Answered By
1 Like