Page 98 - KEC Khaitan C8 Flipbook
P. 98

To call a function


                                  def my_function()                                   Name of a function



                                        print (“Hello”)                                Body of a function




                                  my_function()                                        Function call



                  Program 1: To handle string using user-defined function
                      Program1.py

                   File  Edit  Format   Run    Options   Window    Help

                   #Function definition
                   def hello (name):
                       print('Hello ' +name+ '! How do you do?')

                   #Calling function by passing parameter
                   hello('Yash')




                  You will get the following output:

                      Output

                   Hello Yash! How do you do?



                  Program 2: To add two numbers using user-defined function

                      Program2.py
                   File  Edit  Format   Run    Options   Window    Help

                   #Program that demonstrate to create a user-defined function
                   def add (a, b):
                       c = a+b
                       print("The sum of both the numbers is", c)
                   add (12, 16)



                  You will get the following output:

                      Output

                   The sum of both the numbers is 28









                   96   Premium Edition-VIII
   93   94   95   96   97   98   99   100   101   102   103