KnowledgeBoat Logo

Computer Science

What will be the output of following program ?

num = 1
def myfunc():
    num = 10
    return num
print(num)
print(myfunc())
print(num)

Python

Python Functions

13 Likes

Answer

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.

Answered By

2 Likes


Related Questions