Introduction to Function
Introduction to Functions
• A function is a group of statements that aims to perform a specific
task. For example, in mathematics, a mathematical function f(x)=x2
takes the value of x and returns x2 as output.
Mathematical Function
• Mathematical Function can be implemented straightway as a function
in python. For that, it is necessary to group statements to perform the
square function,
• A function is also known as a procedure, routine, or subprogram.
• The advantage of a function is that, instead of writing a long program,
one can divide that program into a set of functions.
Illustration for use of Functions
Advantages of the function-oriented
approach
Types of Functions
• There are many types of functions.
• The following script illustrates some of the these built-in functions
Anatomy of a User-defined Function
• The syntax of the user-defined function is given below,
• The Components of a function are given below,
1. Function definition
2. Function call
Types of Functions Based on Parameters
Non-Parameterised Function
• A function need not send any information to the called function.
• It is possible to write a python function without any parameters also.
• The following examples.
Parameterised Functions
• A parameterised function passes information as arguments to the
function.
• The following examples illustrates parameterised function where
argument x is passed and its cube is obtained.
Void Functions and Fruitful ( Value Returning)
Functions
• There are two types of functions based on the return values: void
functions and fruitful functions.
Write a python program to compute some
arithmetic operations
Fruitful Functions
• A function can return a value.
• A function that returns a values is called fruitful functions.
• Fruitful functions executes a set of statements given in the function
body and returns a value to the function call.
• A return function can be literal, variable, or expression. One can write
a fruitful function for computing an area of a circle
Types of Function Arguments
• There are five types of functions based on function arguments,
Functions with No arguments
• This type of function has no arguments. There is no need to pass any
arguments.
• Example:
Functions with Positional Arguments
• A functional call is required to invoke the functions.
• A Parameterised function can send a set of arguments to the
function to invoke it.
• These arguments are known by various names such as
1. Positional Arguments
2. Required Arguments
3. Non-keyword arguments
4. Mandatory Arguments
These variables are stored in the parameters for further processing.
Keyword Arguments
• Keyword arguments are also known as named arguments.
• These are the parameters that are identified by name.
• One knowns that the name is assigned by the assignment statement.
• Therefore, one uses an assignment statement for assigning values.
In the above program, the value can be provided using the assignment
as Compute (a=100,b=200)
Default Arguments
• If the value of the arguments is not given at the time of call, the
argument can be initialised with the default value. A default
parameter is a keyword parameter that has a default value. This is a
situation where the value is already known. For example, if the value
of b and c are known already one can default value as compute(a,
b=10, c=30) in functional parameters. So, there is no need to pass the
value for these arguments. The following script illustrates this:
Passing of object Reference
• In most Programming languages, there are two types of
communication between the function caller and function definition.
• They are
1. Pass by value(or call by value)
2. Pass by reference (or call by reference)
Namespaces, global variables, and Scope
• The concept of namespace and scope help to identify variable hierarchy in
the Python context. A namespace is a naming system and it is also known
as context.
• A namespace is a directory, mapping names to values. In other words, a
name is a key to the dictionary. It is mapped to values.
• When a new function is defined, a new namespace is created. All
namespaces can coexist at the same time but are isolated from each other.
Some of the namespaces are as follows.
1) Global namespace: these are the names of the modules
2) Local namespace: the names in the function or function calls
3) Built-in namespace: all the names of the built-in functions and exceptions
Namespace Details
Non-local variables
• Non-local variables are the newest in python.
• The scope of the nonlocal variables in not defined.
• This makes the variable neither global nor local.
• It is neither present in the local scope nor the global scope.
• The keyword creates the nonlocal variables.
• If the non local variables are modifies, they are manifested in the local
variables too.
Conflict Resolution in Scope using the LEGB
Rule
• The types of scopes are given below.
1. Built-in
2. Global
3. Enclosing
4. Local