Page 130 - 2622_Delhi Police Public School_C-8
P. 130

Topic Flashback                                                   21 st  Century   #Information Literacy
                                                                                             Skills
                     1.  Write any two components of Python function.


                     2.  What is the main difference between built-in and user-defined functions?




              CREATING A FUNCTION


              We can create a function in the following ways
                  Defining a Function: We use the def keyword to begin the function definition.

                  Naming a Function: Provide a meaningful name to your function.

                  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>

                          <return statement>

              CALLING A FUNCTION

              A function can be called anytime from other functions or from the command prompt after the definition.
              To call a function, type the function name followed by parentheses. If the function requires parameters,
              include them inside the parentheses. For example:

                               def my_function( ):                                  Name of a function


                                     print ("Hello")                                 Body of a function

                               my_function( )                                        Function call


              Program 1: To print a string by calling a function.

                   Program1.py
                File  Edit  Format   Run   Options   Window    Help


                # Function Example
                def hello(name):
                    print("Hello "+name+"! How do you do?")
                # Calling Function by passing parameter
                hello ('orange')






                        Premium Edition-VIII
                128
   125   126   127   128   129   130   131   132   133   134   135