Functions
A function is named sequence of statement(s) that performs a computation. It contains line of code(s) that are executed sequentially from top to bottom by Python interpreter.
They are the most important building block for any software in Python. They are of 3 types :
Modules : A module is a file containing Python definitions (i.e. functions) and statements. Standard library of Python is extended as module(s) to a Programmer.
User Defined : In Python, it is also possible for programmer to write their own function(s). These functions can then be combined to form module which can be used in other programs by importing them. To define a function, keyword 'def' is used. After the keyword comes an identifier i.e. name of the function, followed by parenthesized list of parameters and the colon which ends up the line, followed by the block of statement(s) that are the part of function.
Built In : Built in functions are the function(s) that are built into Python and can be accessed by Programmer. These are always available and for using them, we don't have to import any module (file).
2.1 Modules
A module is a file containing Python definitions (i.e. functions) and statements. Standard library of Python is extended as module(s) to a programmer. Definitions from the module can be used within the code of a program.
To use these modules in the program, a programmer needs to import the module. Once you import a module, you can reference (use), any of its functions or variables in your code. There are many ways to import a module in your program, the one‟s which you should know are:
i. import
ii. from
import
import is the simplest and most common way to use modules in your code.
Syntax : import module1, module2...
To use/ access/invoke a function, you will specify the module name and name of the function- separated by dot (.). This format is also known as dot notation.
The above code invokes the sqrt (Square root) function from the module math to calculate the square root of the number inserted in the parantheses.
The above code invokes the pow function from the math module, and calculates 2 to the power of 3.
from statement
It is used to get a specific function in the code instead of the complete module file. If we know beforehand which function(s), we will be needing, then we may use from.
Syntax: from module_name import function_name
Math module
We've seen sqrt, and pow functions from the module math previously. Lets look into more functions available in this module.
2.2 Built-in Functions
The Python interpreter has a number of functions that are always available for use. These functions are called built-in functions. For example, print() function prints the given object to the standard output device (screen) or to the text stream file. These are always available and for using them, we don‟t have to import any module (file).
2.3 Composition
Composition is an art of combining simple function(s) to build more complicated ones, i.e., result of one function is used as the input to another.
2.4 User Defined Functions
So far we have only seen the functions which come with Python either in some file (module) or in interpreter itself (built in), but it is also possible for programmer to write their own function(s).
To define a function keyword def is used. After the keyword comes an identifier i.e. name of the function, followed by parenthesized list of parameters and the colon which ends up the line. Next follows the block of statement(s) that are the part of function
Function definition
Calling a function
To call a function, use the function name followed by parenthesis:
Docstring
DocString is an important tool to document the program better, and makes it easier to understand. We can actually access docstring of a function using __doc__ (function name). Also, when you used help(), then Python will provide you with docstring of that function on screen. So it is strongly recommended to use docstring when you write functions.
Parameters and Arguments
Information can be passed to functions as parameter.
Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
Arguments are the value(s) provided in function call/invoke statement. List of arguments should be supplied in same way as parameters are listed
Default Parameter Value
The following example shows how to use a default parameter value.
If we call the function without parameter, it uses the default value:
Return Values
We use the return statement to return values from a function.
Note:
The default value assigned to the parameter should be a constant only.
Only those parameters which are at the end of the list can be given default value.
You cannot have a parameter on left with default argument value, without
assigning default values to parameters lying on its right side.
The default value is evaluated only once, at the point of function definition
print() vs return : What's the difference?
Same as C++, The "print" command outputs text directly to the screen, while the "return" command outputs the values back to whatever is calling the function.
Last updated