Page 117 - Trackpad_V4.0_C8_Flipbook
P. 117

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')






                                                                                   Functions and String  in Python  115
   112   113   114   115   116   117   118   119   120   121   122