Computer Science

What will be the output of following program ?

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

Python

Python Functions

8 Likes

Answer

1
10
10

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 the myfunc function, the value 10 is assigned to the global variable num. Because of the global keyword used earlier, this assignment modifies the value of the global variable num to 10. The function then returns 10.
  5. print(num) — This line prints the value of the global variable num again, which is still 1.

Answered By

2 Likes


Related Questions