Page 116 - Trackpad_V4.0_C8_Flipbook
P. 116

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. We can have functions of the same name 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.








                        Premium Edition-VIII
                114
   111   112   113   114   115   116   117   118   119   120   121