Working With Functions in python
Hi guys in this blog we are going to discuss about Working With Functions in Python.
A Function is a subprogram that acts on data and often returns a value.
Functions make program handling easier as only a small part of the program is dealt with at a time, thereby avoiding ambiguity.
By default, Python names the segment with top-level statements (main program) as _main_.
A Function is executed in an execution frame.
The values being passed through a function-call statement are called arguments (or actual parameters or actual arguments).
The values received in the function definition/header are called parameters (or formal parameters or formal arguments).
Python supports three types of formal arguments parameters (1) Positional arguments (Required arguments). (ii) Default arguments and (iii) Keyword (or named) arguments.
When the function call statement must match the number and order of arguments as defined in the function definition, this is called the positional argument matching.
A parameter having default value in the function header is known as a default parameter. A default argument can be skipped in the function call statement.
The default values for parameters are considered only if no value is provided for that parameter in the function call statement.
Keyword arguments are the named arguments with assigned values being passed in the function call statement.
A function may or may not return a value.
A function may also return multiple values that can either be received in a tuple variable or equal number of individual variables.
A function that returns a non-empty value is a non-void function. Functions returning value are also known as fruitful functions.
A function that does not return a value is known as void function or non-fruitful function. A void function internally returns legal empty value None.
A function in a program can invoke any other function of that program. The program part(s) in which a particular piece of code or a data value (e.g., variable) can be accessed is known as Variable Scope.
In Python, broadly scopes can either be global scope or local scope.
Python resolves the scope of a name using LEGB rule, i.e., it checks environments in the order: Local, Enclosing, Global and Built-in.
A local variable having the same name as that of a global variable, hides the global variable in its function.
The global statement tells a function that the mentioned variable is to be used from global environment.
The global stateme cannot be undone in a code-block i.e., once an identifier is declared global, it cannot be reverted to local namespace.
A function can also return multiple values.
Mutability of arguments/parameter affects the change of value in caller function.