Page 122 - Trackpad_V5_Book 8
P. 122

INTRODUCTION TO PYTHON FUNCTIONS

            It is always advisable to perform a bigger task after breaking it down into a set of manageable

            tasks. This division enables us to manage the task in a more organised manner.

            In a programming language, functions help us to break our program into smaller pieces or modules.
            As our program becomes large and complex, functions make it more organised and manageable.
            Like any other programming language, Python also allows us to use functions in a program.

            A function can be defined as a block of reusable code that performs a specific task. This concept

            is the central aspect on which the concept of procedural programming works.

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


            120   Pro (Ver. 5.0)-VIII
   117   118   119   120   121   122   123   124   125   126   127