Page 73 - Code & Click - 8
P. 73

Functions, Strings, and
               8                                                   Lists in Python







                   Pre-Processing
                   Pre-Processing
                         • Functions in Python                          •  Features of Functions
                         • Components of a Python Function              •  Types of Functions in Python
                         • Creating a Function                          •  Calling a Function
                         • Working with Strings                         •  Working with Lists

            A complex and large program can be broken down into simpler and manageable parts of codes
            called modules. One type of programming modules is called Functions.

            FUNCTIONS IN PYTHON
            A Function is a code block that has a name and can be reused anywhere in a program. A function
            performs a specific task. It can be used in a program by simply using its name. The print( ) and input( )
            are examples of functions that you have already used in various programs in Python.

            FEATURES OF FUNCTIONS
            Functions are the basic components of a procedural programming language. Some important fea-
            tures of functions are:
              1.  Modularity: Functions let us divide a large, complex program into small parts called modules.
                  This helps in keeping the code organised and easy to make changes in it.
              2.  Reusability: A function can be used as many times as required in a program. This saves time
                  and effort of writing the same code repeatedly. It also helps reduce the length of a program.
              3.  Easy Debugging: It is easy to track errors in a program and correct them when it is divided into
                  smaller, manageable segments.

            COMPONENTS OF A PYTHON FUNCTION
            A Python program is made up of the following components:
               •  Function Name: A function is given a name that is unique and related to its purpose.
               •   Arguments: The values given as input to the function are called Arguments. A function may or
                  may not have any arguments.
               •  Body of Function: It consists of all the executable statements that are used to perform the task
                  of the function.
               •   Return Expression: A function may or may not return a value as output.

            For example, consider the code for a function given below:
                   def area_square(side):
                          area = side * side
                          return area
            Here, the name of the function is area_square. It accepts one argument – side. It calculates the area
            of the square. The value of area is returned by the return statement.


                                                                                                                 71
   68   69   70   71   72   73   74   75   76   77   78