What is a Function
A Function is a set of codes that are design to perform a single or related task, it provide modularity and increase code reusability. A function executes only when t is being called. Python provides many build in function like print(), len(), type()..etc, whereas the functions created by us, is called User Defined Functions.When a function is called for execution, Data can be passes to the function called as function parameters. After execution the function may return data from the function.
Benefits of using Function
Code Reusability: A function once created can be called countless number of times.
Modularity: Functions helps to divide the entire code of the program into separate blocks, where each block is assigned with a specific task.
Understandability: use of Functions makes the program more structured and understandable by dividing the large set of codes into functions
Procedural Abstraction: once a function is created the programmer doesn’t have to know the coding part inside the functions, Only by knowing how to invoke the function, the programmer can use the function.
Types of Functions in Python
Built-in Function:
Ready to use functions which are already defined in python and the programmer can use them whenever required are called as Built-in functions. Ex: len(), print(), type()..etc
Functions defined in Modules:
The functions which are defined inside python modules are Functions defined in modules. In order to use these functions the programmer need to import the module into the program then functions defined in the module can be used.
import math
p=math.pow(5,2)
print(p)
User Defined Function
Functions that are defined by the programmer to perform a specific task is called as User Defined Functions. The keyword def is used to define/create a function in python.
Elements in Function Definition
- Function Header: The first line of the function definition is called as function header. The header start with the keyword def, and it also specifies function name and function parameters. The header ends with colon ( : ). def sum(a,b): #function Header
- Parameter: Variables that are mentioned inside parenthesis ( ) of the function Header.
- Function Body: The set of all statements/indented-statements beneath the function header that perform the task for which the function is designed for.
- Indentation: All the statements inside the function body starts with blank space (convention is four statements).
- Return statement: A function may have a return statement that is used to returning values from functions. A return statement can also be used without any value or expression.
Flow of Execution in a Function Call
Arguments and parameters in Function
In Python, the variables mentioned withing parathesis () of a function definition are referred as parameters(also as formal parameter/formal argument) and the variables present inside parathesis of a function call is referred as arguments (also as actual parameter/actual argument). Python supports 4 types of parameters/formal arguments
-
Positional Argument
-
Default Argument
-
Keyword Argument
-
Variable length argument
Positional Arguments:
When function call statement must contain the same number and order of arguments as defined in the function definition, then the arguments are referred as positional argument.
]
Default Argument
Default values can be assigned to parameter in function header while defining a function, these default values will be used if the function call doesn’t have matching arguments as function definition.
*Note: non-default arguments cannot follow default arguments
Keyword Argument
Python allows to call a function by passing the in any order, in this case the programmer have to spcify the names of the arguments in function call.
Variable Length Argument
Variable Length Argument in python permits to pass multiple values to a single parameter. The variable length argument precedes the symbol ‘*’ in the function header of the function call.
Returning Values from Function
Functions in python can return back values/variables from the function to the place from where the function is called using return statement. Function may or may not return values, python function can also return multiple values.
return statement terminates the function execution if it is encountered.
Scope of Variables
Part(s) of the program where the variable is legal and accessible is referred as its scope. Based on scope, variable are categorized into 2 categories
Local Variable: Variable defined inside a function is called as Local Variable. Its scope is limited only within the function in which it is defined.
Global Variable: Variable defined outside all function is called as Global variable.
Using a Global variable in local scope:
To access a variable declared outside all functions(Global Variable) in a local scope then global statement is used.
When global statement is used for a name, it restrict the function to create a local variable of that name instead the function uses the global variable.