Page 123 - Trackpad_V5_Book 8
P. 123

User-Defined Functions

            User-defined functions are created by the user according to the need of the program. Once the
            user defines a function, the user can call it in the same way as the built-in functions. User-defined
            functions  are divided  into  various  categories  based  on the  parameters  and return type.  The
            functions that do not take any parameter or return anything are called type 1 functions.

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

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




























            You will get the following output:




















            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.





                                                                              Functions, String and List in Python  121
   118   119   120   121   122   123   124   125   126   127   128