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

04     Input Parameters: None
                05     Return value: None
                06     '''
                07     print("     *  ")
                08     print("    ***  ")
                09     print("   *****  ")
                10     print("  *******  ")
                11
                12 def rhombus():
                13     '''
                14     Objective: To print the rhombus
                15     Input Parameters: None
                16     Return value: None
                17     '''
                18     print("     *  ")
                19     print("    ***  ")
                20     print("   *****  ")
                21     print("  *******  ")
                22     print("   *****  ")
                23     print("    ***  ")
                24     print("     *  ")
              When Program 8.2a is executed, the IDLE responds with no output.



                       Why does the program not print a triangle and a rhombus?




              8.2.1 Calling a Function

              Now, we understand that a function definition only provides us a with provision to do something in a program. It
              comes into action (i.e. gets executed) only when it is invoked. Invoking a function is also called a function call. So, just
              defining a function in a program does not achieve any result unless it is invoked.


                     A function is executed only when it is invoked. Defining a function ONLY makes a provision to do something.


              Next, we use the above definitions of the functions triangle() and rhombus() in Program 8.2b.  The program
              performs the following tasks:

              1.  Calls a user-defined function triangle(), to print a triangle.
              2.  Leaves a blank line of output.
              3.  Calls user-defined function rhombus() to print a rhombus.
              4.  Leaves a blank line of output.

              5.  Calls user-defined function triangle()to print a triangle.

               Program 8.2b: Program to display the triangle followed by a rhombus using functions (pattern2.py)
                01 def triangle():
                02     '''
                03     Objective: To print the triangle
                04     Input Parameters: None
                05     Return value: None
                06     '''



               200   Touchpad Computer Science (Ver. 2.0)-XI
   209   210   211   212   213   214   215   216   217   218   219