Python Function
A function is a block of organized, reusable code that is used to perform a single,
related action.
Defining a Function
Function blocks begin with the keyword def followed by the function name
and parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses
The code block within every function starts with a colon (:) and is indented.
The statement return value(literal/expression/Boolean) to the caller.
A return statement with no arguments is the same as return None.
There are two types of function in Python programming:
Build-In /Standard library functions - These are built-in functions in Python
that are \ available to use.
User-defined functions – Developer can create own functions based on
requirements.
To call a function, use the function name followed by parenthesis:
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that is sent to the function when it is called.
By default, a function must be called with the correct number of arguments.
Function Arguments
You can call a function by using the following types of formal arguments −
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Required arguments
Required arguments are the arguments passed to a function in correct positional
order. Here, the number of arguments in the function call should match exactly
with the function definition.
Keyword arguments
Keyword arguments are related to the function calls. When you use keyword
arguments in a function call, the caller identifies the arguments by the parameter
name.
Default arguments
A default argument is an argument that assumes a default value if a value is not
provided in the function call for that argument.
Scope of Variables
All variables in a program may not be accessible at all locations in that program.
This depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can
access a particular identifier. There are two basic scopes of variables in Python −
Global variables
Local variables
Global vs. Local variables
Variables that are defined inside a function body have a local scope, and those
defined outside have a global scope.
This means that local variables can be accessed only inside the function in which
they are declared, whereas global variables can be accessed throughout the
program body by all functions.