Page 153 - Plus_V2.2_C8_Flipbook
P. 153

FUNCTIONS


                 A  function  is  a  block  of  organised  and  reusable  code  used  to  perform  a  single  or related  action.
                 Functions receive data in the form of arguments and use it to run a specified set of statements and
                 produce the final output.

                      Features of Function

                 Functions are the basis of procedural programming. Some important features of functions are:
                    A  program is  divided  into  small modules  and  each  module  performs some  specific  task.  Each
                   module can be called as per the requirement.

                    We can call a function as many times as required. This saves the programmer the time and effort
                   to rewrite the same code again. Therefore, it also reduces the length of the program.
                    Dividing a bigger program into smaller functions makes the program more manageable. It makes
                   debugging easy and makes the code efficient, both in terms of time and memory.


                      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.
                   Arguments: The input given to the functions are referred to as arguments. A function can or cannot
                   have any arguments.
                    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 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

                 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.
                 Type 2 functions take parameters but do not return anything. Type 3 functions take parameters and
                 return output.


                                                                                      Functions and String in Python  151
   148   149   150   151   152   153   154   155   156   157   158