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

Fig 8.5: interest(1000, 5, 2): call to function interest() from IDLE
                 8.2.3 Help on a User-defined Function

                 When we invoke the help() function on a user-defined function, it displays the function name along with the
                 parameter list and the first doc string (if  present) in the function that appears before any executable statements, for
                 example,
                  >>> help(interest)
                          Help on function interest in module __main__:
                             interest(principal, rate, time)
                        Objective: To determine simple interest
                        Input Parameters:
                           principal- numeric value denoting principal amount
                      rate - numeric value denoting a rate of interest in % per annum
                      time - numeric values indicating the time period in years
                      Return value: float-simple interest

                       1.  Positional arguments-The arguments in a function call that necessarily appear in the same order in which the
                          formal parameters are specified in the function definition.
                       2.  Formal parameters in the function definition and the arguments in a function call can have the same name.
                       3.  return is a keyword that is used to return the result of computation from a function to the statement that
                          invoked the function.

                       4.  In the absence of any return statement, a function returns the default value None.
                 Functions are the fundamental building blocks of larger programs. In the upcoming chapter, we will
                 explore modules, which allow us to organize these building blocks into files and reuse them across
                 programs and projects. This approach makes our code more structured, maintainable, and powerful.


                      Let's Summarise

                   Execution of a program with user-defined functions

                   Ø   When a function is invoked, the control flow changes as shown in Fig 8.6.
                                                    Sequence Execution

                                                    3 def test (num):
                                                    4          num = num + 1
                                                    5          return num
                                                    1 var = 10
                                                    2 setNum = test (var)

                                                    6 print (setNum)
                                          Fig 8.6: Flow of execution in a program with user-defined functions
                                                                                             Introduction to Functions  207
   216   217   218   219   220   221   222   223   224   225   226