Python Functions
Python Functions
A Python function is a block of organized, reusable code that is used to perform a single, related
action. Functions provide better modularity for your application and a high degree of code
reusing.
A top-to-down approach towards building the processing logic involves defining blocks of
independent reusable functions. A Python function may be invoked from any other function by
passing required data (called parameters or arguments). The called function returns its result
back to the calling environment.
Built-in functions
1 Python's standard library includes number of built-in functions. Some of Python's built-in
functions are print(), int(), len(), sum(), etc. These functions are always available, as they
are loaded into computer's memory as soon as you start Python interpreter.
2 The standard library also bundles a number of modules. Each module defines a group of
functions. These functions are not readily available. You need to import them into the
memory from their respective modules.
User-defined functions
3 In addition to the built-in functions and functions in the built-in modules, you can also
create your own functions. These functions are called user-defined functions.
Example
Let's modify greetings function and have name an argument. A string passed to the function as
actual argument becomes name variable inside the function.
def greetings(name):
"This is docstring of greetings function"
print ("Hello {}".format(name))
return
greetings("Samay")
greetings("Pratima")
greetings("Steven")
This code will produce the following output −
Hello Samay
Hello Pratima
Hello Steven
Types of Python Function Arguments
Based on how the arguments are declared while defining a Python function, they are classified
into the following categories −
Positional or Required Arguments
Keyword Arguments
Default Arguments
Positional-only Arguments
Keyword-only arguments
Arbitrary or Variable-length Arguments
Example 2
The following example gives more clear picture. Note that the order of parameters does not
matter.
# Function definition is here
def printinfo( name, age ):
"This prints a passed info into this function"
print ("Name: ", name)
print ("Age ", age)
return;
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.
Example
The following example gives an idea on default arguments, it prints default age if it is not passed
# Function definition is here
def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print ("Name: ", name)
print ("Age ", age)
return;
Keyword-only arguments
Those arguments that must be specified by their name while calling the function is known
as Keyword-only arguments. They are defined by placing an asterisk ("*") in the function's
parameter list before any keyword-only parameters. This type of argument can only be passed to
a function as a keyword argument, not a positional argument.
Example
In the code below, we have defined a function with three keyword-only arguments. To call this
method, we need to pass keyword arguments, otherwise, we will encounter an error.
def posFun(*, num1, num2, num3):
print(num1 * num2 * num3)
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. When you call a function, the variables declared inside it are brought into scope.
Example
Following is a simple example of local and global scope −
total = 0; # This is global variable.
# Function definition is here
def sum( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2; # Here total is local variable.
print ("Inside the function local total : ", total)
return total;