Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 85 - 2607_Traclpad_Pro_5.1_flipbook_C8
P. 85

    User-Defined Functions

                      User-defined functions are created by the user according to the needs of the program. Once the
                      user defines a function, the user can call it in the same way as the built-in functions. The main
                      difference between these two categories is that built-in functions do not require to be written
                      by  us, whereas  a user-defined  functions  are created  by  the  programmer  to  address  specific
                      requirements or perform custom tasks within a program.

                      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.

                         Parameters: The parameters (separated by commas) are specified within 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.
                        The syntax of creating a function is as follows:

                        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 name 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

                      User-defined functions are divided into various categories based on their parameters and return
                      type. The functions that do not take any parameters and do not return any values are called type
                      1 functions.

                      The type  2 functions  take parameters  but  do  not  return anything.  The type  3 functions  take
                      parameters and return values. Let us understand this with the help of an example:

                       84   Premium Edition-VIII

| 80 81 82 83 84 85 86 87 88 89 90 |