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

ADVANTAGES OF FUNCTIONS

                 Following are the advantages of functions:
                     You can write a 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 organised and manageable.
                     The complexity of a program can be divided into simple subtasks and function subprograms can be
                    written for every subtasks.

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

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

                 COMPONENTS OF PYTHON FUNCTION


                 A Python function consists of the following components:
                    Name of the function: A function name should be unique and easy to correlate with the task it will
                    perform. Multiple functions cannot have the same name even with different parameters.

                    Parameters: These are the variables given inside the parentheses in the function definition.
                    Statements: The statements are the executable instructions that the function can perform.
                    Return Value: A function may or may not return a value.


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

                 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. 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 a value.








                                                                                   Functions and String  in Python  127
   124   125   126   127   128   129   130   131   132   133   134