Page 58 - tp_Modula_v2.0
P. 58

Supply Parameters: The parameters (separated by commas) are given in the parenthesis

                      following the name of the function. These are basically the input values we pass to the function.
                      Body of the function: The body of the function contains Python statements that make our
                      function perform the required task. Syntax of creating a function is:

                      def < name of the function > (list of parameters)

                              <body>

                      CALLING A FUNCTION

                  A function can be called anytime from other functions or from the command prompt after the
                  definition. For calling a function, we type the function and pass the parameters. For example:

                  To call a function.


                                 def my_function( ):                                 Name of a function



                                       print("Hello")                                 Body of a function


                                 my_function( )                                        Function call


                  Program 1: To add two numbers using all the three types of built-in functions.
                      Program1.py

                   File  Edit  Format   Run   Options  Window    Help

                   # Typel Function:
                   def add1():
                       a = int (input("Enter the first number: "))
                       b = int (input("Enter the second number: "))
                       c = a+b
                       print("The sum of both the numbers is " + str(c))

                   # Type2 Function:
                   def add2(a, b):
                       c = a+b
                       print("The sum of both the numbers is " + str(c))

                   # Type3 Function:
                   def add3(a, b):
                       c = a+b
                       return (c)
                   print("Calling Typel function")
                   add1()
                   print("Calling Type2 function")
                   add2 (12, 16)
                   print("Calling Type3 function")
                   result = add3 (20,30)
                   print(result)



                   56     Touchpad MODULAR (Version 2.0)
   53   54   55   56   57   58   59   60   61   62   63