CBSE Class 12 Computer Science Question 46 of 101

Functions — Question 14

Back to all questions
14
Question

Question 13(ii)

What will be the output of the following program?

num = 1
def myfunc() :
    num = 10 
    return num
print (num) 
print (myfunc())
print (num)
Answer
Output
1
10
1
Working
  1. num = 1 — This line assigns the value 1 to the global variable num.
  2. def myfunc() — This line defines a function named myfunc.
  3. print(num) — This line prints the value of the global variable num, which is 1.
  4. print(myfunc()) — This line calls the myfunc function. Inside myfunc function, num = 10 defines a local variable num and assigns it the value of 10 which is then returned by the function. It is important to note that the value of global variable num is still 1 as num of myfunc is local to it and different from global variable num.
  5. print(num) — This line prints the value of the global variable num, which is still 1.