Page 218 - Computer Science V2.0 Class 11
P. 218
Next, the control returns to the global frame. Now all statements in the global frame have already been executed
(also indicated by the grey arrow which marks the statement just executed), the program terminates.
8.2.2 Computing Simple Interest
We wish to write a program that computes simple interest for the given principal amount, rate of interest per cent per
annum, and time in years.
● To compute the simple interest, we develop a user-defined function interest() (see Program 8.3a).
● Line 1 defines the function header for the function interest().
● Note that the function definition begins with the keyword def, followed by the name of the function-interest,
followed by the dummy arguments (to be used for computation) within parenthesis, and followed by a colon at the
end.
● Lines 2–11, comprise a sequence of statements forming the function's body.
● Lines 2–9 span a multi-line string (called a docstring). The docstring is followed by the statement that computes
simple interest (line 10).
● Finally, there is a return statement that returns the result of the computation to the statement that invoked the
function.
Program 8.3a Computation of simple interest
01 def interest(principal, rate, time):
02 '''
03 Objective: To determine simple interest
04 Input Parameters:
05 principal- numeric value denoting principal amount
06 rate - numeric value denoting rate of interest in % per annum
07 time - numeric values indicating time period in years
08 Return value: float-simple interest
09 '''
10 simpleInterest = (principal*rate*time)/100
11 return simpleInterest
Now, we will make use of the above definition of the function interest() to compute simple interest for a specified
amount, interest rate, and time (Program 8.3b, lines 22–27).
Program 8.3b Computation of simple interest
01 def interest(principal, rate, time):
02 '''
03 Objective: To determine simple interest
04 Input Parameters:
05 principal- numeric value denoting principal amount
06 rate - numeric value denoting rate of interest in % per annum
07 time - numeric values indicating time period in years
08 Return value: float-simple interest
09 '''
10 simpleInterest = (principal*rate*time)/100
11 return simpleInterest
12
13 #main program segment
14 '''
15 Objective: To compute simple interest
16 User Interface:
17 1. User is asked to enter:
18 principal amount, rate of interest, time period of interest
19 2. Output to the user:
20 Simple interest computed
204 Touchpad Computer Science (Ver. 2.0)-XI

