Page 219 - Computer Science V2.0 Class 11
P. 219
21 '''
22 p = int(input('Enter the principal amount: '))
23 r = int(input('Enter the rate of interest: '))
24 t = int(input('Enter the time period: '))
25 assert p>=0 and r>=0 and t>=0
26 simpleInterest = interest(p, r, t)
27 print('Simple Interest:', simpleInterest)
Recall that a Python program is a global frame in which functions and statements appear (see Fig 8.4a). When the
code in the Python module interest.py is executed, Python encounters the definition of function interest()
(lines 1–11) )in the global frame and makes a note of it, as shown in Figure 8.4a.
Fig 8.4a: Python notes of the definition of the function interest().
● On execution of lines 22–24, Python takes user inputs for the principal amount (p), rate of interest (r),
and time period (t).
● Let us assume p, r, and t take values 1000, 5, and 2, respectively.
● Execution of line 25 validates the inputs. Statement assert passes the control to the following statement (line 26)
only if the condition mentioned in the statement holds True, otherwise, flags an error.
● Line 26 is an assignment statement. The evaluation of the expression on the right-hand side of the assignment
operator results in a call to the function interest().
● As before, instead of saying we call the function interest(), we can say that we invoke the function interest().
Thus, the control is transferred to line 1 of Program 8.3b where the definition of the function interest() begins.
● The values 1000, 5, and 2 are passed as inputs to the function interest() and are assigned to the formal
parameters (also known as dummy parameters) principal, rate, and time (see Fig 8.4b).
Note that a call to the function comprises the name of the function, followed by a pair of parentheses that includes
(optionally) a comma-separated sequence of arguments:
function_name([comma_separated_sequence_of_arguments])
Further, note that the arguments in the function call appear in the same order in which formal parameters appear in
the function definition.
Fig 8.4b: Call to function interest() with parameters: 1000, 5, 2
Introduction to Functions 205

