Page 57 - tp_Modula_v2.0
P. 57

ADVANTAGES OF FUNCTIONS

                 Following are the advantages of functions:

                     You can write Python program in logically independent sections.
                    Functions provide better modularity for your application and a high degree of code reusing.

                    As the program grows larger, functions make it more organized and manageable.
                     The complexity of a program can be divided into simple subtasks and function subprograms
                     can be written for every subtasks.

                     Multiple persons can work on same program by assigning different functions to each of them.
                    In Python, a function can call the same function again and again, called as recursiveness.

                     You could also bring in other libraries or modules to your program that contain pre-defined
                     functions readily available for use.

                     TYPES OF FUNCTIONS IN PYTHON

                 We have mentioned earlier that one of the strengths of the Python language is that Python
                 functions are easy to define and use. Python functions can be categorised into built-in functions
                 and user-defined functions. Let us discuss about them in detail.

                 Built-In Functions


                 The print( ) and input( ) belong to the category of built-in functions. We also have other built-
                 in functions like range( ), type( ), etc. The main difference between these two categories is that
                 built-in functions do not require to be written by us whereas a user-defined function has to be
                 developed by the user at the time of writing a program.

                 User-Defined Functions
                                                                       Tech
                 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         Funda
                 it in the  same way  as the  built-in functions.                         def keyword is used to

                 User-defined functions are divided into various                          define the functions.
                 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.

                     CREATING A FUNCTION


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

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

                                                                                            Functions in Python   55
   52   53   54   55   56   57   58   59   60   61   62