Python Programming – Python Functions
In programming, sometimes a similar code has to be repeated at many places in a program. For which, the programmer needs to write the same set of instructions several times. For example, while computing a binomial coefficient, the factorial needs to be computed several times. It would waste the programmer’s time and effort to write the same code again and again. This activity not only increases the size of the program but also leads to typographical errors.
To overcome this issue, the Python programming language is provided with a feature called subprograms or functions. A subprogram or function is a name given to a set of instructions that can be called by another program or a subprogram. This technique simplifies the programming process to a great extent because a single set of instructions in the form of a subprogram is being used for all instances of computational requirements with only changes in the supplied data.
In the Python programming language, a function is a self-contained group of statements, which are intended to perform a specific task. Functions break the program into smaller modules, which perform a particular operation and are easy to understand. The large programs become more manageable and organized by using the concept of functions.
Advantages of Functions
1. Avoids Repetition: functions avoid repetition of the similar code again and again. The same code of a function can be used several times by different parts of the program or by different programs.
2. Modularization: decomposing a large program into smaller modules makes the designing of the program faster and easier. Different programmers can work on different modules, which can be further combined to make the larger complete program.
3. Reduce Size: the size of the source program can be reduced considerably by using functions appropriately in the program.
4. Logical Precision: the use of user-defined functions allows a large. program to be broken into smaller self-contained blocks or
components, which is intended to perform a specific and unique function. By the use of functions, the program becomes easier to code and debug.
5. Save Memory Requirement: a different set of data can be transferred to the function each time when it is called. The use of
functions saves huge memory space by allowing the same set of statements to be used for different sets of data.
6. Portable: functions can be used in many programs, which encourages portability.
Types of Functions
Alike C/C++/Java, Python functions can be divided into the following two types:
- Built-in functions – Functions that are built into Python.
- User-defined functions – Functions defined by the users themselves.
In this chapter, we have learned about one of the best constructs of the Python programming language known as functions. The two types of functions built-in and user-defined are described. As Python supports a huge library of built-in functions a complete list is provided with the description of each. Subsequently, user-defined functions are explained in-depth with then- various types along with the illustration of each. Anonymous functions, which are developed with the lambda function are elaborated in detail. The Python pass by value and pass by object reference are also described with their respective illustrations. One of the most exciting types of functions known as recursion is described. In the end, the chapter concludes by describing the scope and lifetime of variables.