[Type B] Working with Functions – Chapter 3 Sumita Arora
Table of Contents
3. Consider the following code and write the flow of execution for this. Line numbers have been given for your reference.
Python
def power(b, p):
y = b ** p
return y
def calcSquare(x):
a = power(x, 2)
return a
n = 5
result = calcSquare(n)
print(result)
Solution:
power
function is defined.calcSquare
function is defined.n
is assigned the value5
.calcSquare
function is called with argumentn
.- Inside
calcSquare
,power
function is called with argumentsx
and2
. power
function calculates the square ofx
.- The result is returned and assigned to
result
. result
is printed.
In short, flow of execution is as follows:
- Define
power
function.- Define
calcSquare
function.- Assign
n = 5
.- Calculate square of
n
usingcalcSquare
.- Print the result.