Computer Science
Consider the following code and write the flow of execution for this. Line numbers have been given for your 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)
Python Functions
27 Likes
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.
Answered By
11 Likes
Related Questions
What are the errors in following codes ? Correct the code and predict output :
def Tot(Number) #Method to find Total Sum = 0 for C in Range (1, Number + 1): Sum += C RETURN Sum print (Tot[3]) #Function Calls print (Tot[6])
Find and write the output of the following python code :
def Call(P = 40, Q = 20): P = P + Q Q = P - Q print(P, '@', Q) return P R = 200 S = 100 R = Call(R, S) print(R, '@', S) S = Call(S) print(R, '@', S)
What will the following function return ?
def addEm(x, y, z): print(x + y + z)
What will the following function print when called ?
def addEm(x, y, z): return x + y + z print(x + y + z)