CBSE Class 12 Computer Science Question 42 of 101

Functions — Question 10

Back to all questions
10
Question

Question 10

Consider the following code and write the flow of execution for this. Line numbers have been given for our reference.

1. def power(b, p):
2. y = b ** p
3. return y
4. 
5. def calcSquare(x):
6. a = power(x, 2)
7. return a
8. 
9. n = 5
10. result = calcSquare(n)
11. print(result)
Answer

The flow of execution for the above program is as follows :

1 → 5 → 9 → 10 → 5 → 6 → 1 → 2 → 3 → 6 → 7 → 10 → 11

Explanation

Line 1 is executed and determined that it is a function header, so entire function-body (i.e., lines 2 and 3) is ignored. Line 5 is executed and determined that it is a function header, so entire function-body (i.e., lines 6 and 7) is ignored. Lines 9 and 10 are executed, line 10 has a function call, so control jumps to function header (line 5) and then to first line of function-body, i.e., line 6, it has a function call , so control jumps to function header (line 1) and then to first line of function-body, i.e, line 2. Function returns after line 3 to line 6 and then returns after line 7 to line containing function call statement i.e, line 10 and then to line 11.