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

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

                Ø   The program/function that calls a function is called function caller. The called function is  called the callee
                    function.

                Ø   A function may have:
                       no argument and no return value
                       argument(s) but no return value
                       argument(s) as well as return value

                Ø   A function must be defined before it is called. If the function call precedes the function definition, Python
                    interpreter will point out an error.
                Ø   When the interpreter encounters the function definition (def statements), the statement(s) in the function
                    body is (are) not executed at that time. Python only makes a note of the function definition.
                Ø   A function is executed only when it is called. When a function is called, the control shifts to the first statement
                    of the function body.
                Ø   When the control reaches the end of the function body, or a return statement is encountered, the control
                    returns to the point from where the function was called.





                                                    Solved Exercises


              A.  Multiple Choice Questions.
                 1.  Which of the following is NOT correct about a function?
                    a.  There can be only one user-defined function in a program
                    b.  A function may or may not return a value
                    c.  A function header always ends with a colon(:)
                    d.  The function body is executed every time a function is called

                 2.  Which of the following types of functions are created and used by the programmer?
                    a.  Built-in functions
                    b.  User-defined functions
                    c.  Both a and b
                    d.  Neither a nor b
                 3.  Which of the following is the correct function header?
                    a.  Def test():          b. def test()          c. def test():        d. DEF test()


               208   Touchpad Computer Science (Ver. 2.0)-XI
   217   218   219   220   221   222   223   224   225   226   227