Page 220 - Computer Science V2.0 Class 11
P. 220

Lines  2–9  in  the  function  interest()  are  for  documentation  purpose.  Execution  of  line  10  computes
              simpleInterest. Note that the return value refers to simpleInterest (see Fig 8.4c).























                                    Fig 8.4c: Return value (100.00): just before the return statement is executed
              When the return statement is executed, the execution of the function comes to end and Python returns the control
              to line 26, and the execution of the assignment statement assigns the value 100.0 just returned by the function
              interest() (see Fig 8.4d). Finally, the execution of line 27 prints the following value of simpleInterest, and
              the execution of the program completes.

              Simple Interest: 100.0






















                Fig 8.4d:Execution of the assignment statement (line 26) completes as function interest() returns value 100 to the caller.

              When Program interest.py is executed in the Python's IDLE environment, the definition of the function becomes
              interest() becomes available for use in the Python shell, and we can invoke it directly as shown below:

               >>> interest(1000, 5, 2)
                    100.0

                     Function Call: Used to invoke a function, using its name, followed by the arguments (if any) in parentheses.
                     Formal Parameters: Variables that receive the values during a call to the function.
                     Input Arguments: Input values passed during the call to a function


              Suppose we wish to compute simple interest for the principal amount of Rs. 1000, rate 5% and time 2 years. The
              execution of the function begins by transferring the control to line 1, and the arguments 1000, 5, 2 are to be
              passed to the function interest(). Skipping non-executable multi-line comment, execution of line 10 computes
              the simple interest, and line 11 returns the value of simpleInterest to the caller (Python shell in this case, see
              Fig 8.5), which displays the value returned by the function interest().


               206   Touchpad Computer Science (Ver. 2.0)-XI
   215   216   217   218   219   220   221   222   223   224   225