You can learn about Functions in Python Programs with Outputs helped you to understand the language better.
Python Programming – Functions
A function is a routine, or set of instructions, that performs a specific task and can be processed independently. When the program passes control to a function, the function performs that task and returns control to the instruction following the calling instruction. Python function is a grouping of program statements put into a single unit to carry out tasks at a given level. Each Python function can be activated through the execution of a function call.
- The collection of functions creates a. program. The function is also known as procedure or subroutine in other programming languages.
A function can be equated to a “black box” to which one or more input values are passed, some processing is done, and the output value is then returned automatically. Figure 10.4 illustrates this sequence for the call to a function sqrt. The value of x(16.0) is the function input, and the function result or output is the square root of 16.0, i.e., 4 (See Figure 10.4).
Suppose you want to calculate the Total marks of various students in a class. If you write a program without using the function, you have to write the same code again and again. But if you use a function say total, then you need not write the code again and again. Instead, you can jump to the section of code that calculates Total marks and come back to normal program flow when you have finished.
Advantages of using Function
Functions in Python are used because of the following reasons:
a. The use of functions enables you to write Python programs is logically independent sections in the same way as you develop the solution algorithm. So, functions provide better modularity for your application and a high degree of code reusing.
b. Functions may be executed more than once in a program. Also if you have written and tested a function subprogram, you may use it in other programs or other functions. By using functions, you can avoid rewriting the same logic/code again and again in a program. So a function can be called a section of a program that is written once and can be executed whenever required in the program, thus making code reusability.
c. As the program grows larger and larger, functions make it more organized and manageable. You can track a large Python program easily when it is divided into multiple functions i.e., the complexity of a program can be divided into simple subtasks and function subprograms can be written for every subtask. The subprograms are easier to write, understand and debug.
d. Multiple persons can work on the same program by assigning different functions to each of them. Programmers reduce coding time and debugging time, thereby reducing overall development time, by using functions.
e. In Python, a function can call the same function again. It is called recursiveness. Many calculations can be done easily using recursive process, such as calculation of factorial of a number.
f. You could also bring in other libraries or modules to your program that contain pre-defined functions readily available for use. Built-in functions are the function(s) that are built into Python and can be accessed by a programmer. For example len ( ), type ( ), int ( ), etc. A library of functions can be designed and tested for use of every programmer.
The most important reason to use functions is to make program handling easier as only a small part of the program is dealt with at a time. It also reduces the size of the program and makes the program more readable and understandable.
Functions vs Methods
A function just refers to a standalone function. Functions can be called only by its name, as it is defined independently. A method refers to a function that is part of a class. You can access it with an instance or object of the class. Methods can’t be called by their name only, you need to invoke the class by a reference of that class in which it is defined, i.e., the method is defined within a class and hence they are dependent on that class. So all methods are functions, but not all functions are methods.
# An example of a Python function a = ‘Welcome’
something.method ( )
# An example for a Python method a.upper ( )
Relation between Module, Package, and Library A module is a file containing Python definitions, functions, variables, classes, and statements with .py extension which you intend to reuse in different codes of your program. To reuse the functions of a given module you simply need to import the module.
A Python package refers to a directory of Python module(s). This feature comes in handy for organizing modules of one type at one place. You can think of packages as the directories on a file system and modules as files within directories. For example, NumPy is a Python package for scientific computing which can deal with large data sizes. The term library is simply a common term for a bunch of code that was designed with the aim of being usable by many applications. It provides some common functionality that can be used by specific applications.
In Python, a library is used loosely to describe a collection of the core modules. The term ‘standard library’ in Python language refers to the collection of exact syntax, token, and semantics of the Python language which comes bundled with the core Python distribution. The Python installers for Windows automatically add the standard library and some additional libraries.
- Script means series of commands within a single file.