Chapter 4 Python
Chapter 4 Python
Python
Programming
Faculty of Computing
1
Chapter Four
Function
2
What is function?
Functions are the most important aspect of an application.
A function: can be defined as the organized block of reusable code,
which can be called whenever required.
Python allows us to divide a large program into the basic building
blocks known as a function.
The function contains the set of programming statements enclosed
by {}.
A function can be called multiple times to provide reusability and
modularity to the Python program.
3
What is function?
It organizes the code very effectively and avoids the repetition of the
code.
As the program grows, function makes the program more organized.
Python provide us various inbuilt functions like range() or print().
4
Types of Function
There are mainly two types of functions.
User-define functions - The user-defined functions are those define by
the user to perform the specific task.
Example: A user can create a function that can encrypt a plain text
Built-in functions - The built-in functions are those functions that are pre-
defined in Python.
Example: print(), range(), math.sqrt(), input(), int(), str(), etc.
5
Advantages of function
Using functions, we can avoid rewriting the same logic/code again
and again in a program.
We can call Python functions multiple times in a program and
anywhere in a program.
We can track a large Python program easily when it is divided into
multiple functions.
Reusability is the main achievement of Python functions.
However, Function calling is always overhead in a Python program.
6
Creating a function
In Python a function is defined using the def keyword.
Syntax
7
Creating a function
Let's understand the syntax of functions definition.
The def keyword, along with the function name is used to define the
function.
The identifier rule must follow the function name.
A function accepts the parameter (argument), and they can be optional.
The function block is started with the colon (:), and block statements must
be at the same indentation.
The return statement is used to return the value. A function can have only
one return statement
8
Calling a function
In Python, after the function is created, we can call it from another
function. A function must be defined before the function call;
otherwise, the Python interpreter gives an error.
To call the function, use the function name followed by the
parentheses.
Syntax:
name_of_function(argument_list)
9
Defining and Calling a function
10
Defining and Calling a function
Output
11
Function arguments
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.
The following are the types of arguments that we can use to call a
function:
Default arguments
Keyword arguments
Required arguments
Variable-length arguments
12
Default arguments
A default argument is a kind of parameter that takes as input a
default value if no value is supplied for the argument when the
function is called.
Output
13
Keyword arguments
The arguments in a function called are connected to keyword
arguments. If we provide keyword arguments while calling a
function, the user uses the parameter label to identify which
parameters value it is.
Since the Python interpreter will connect the keywords given to link
the values with its parameters, we can omit some arguments or
arrange them out of order.
14
Keyword arguments
Output
15
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.
16
Required arguments
Output
17
Variable-Length arguments
We can use special characters in Python functions to pass as many
arguments as we want in a function. There are two types of
characters that we can use for this purpose:
*args - these are Non-Keyword Arguments
**kwargs - these are Keyword Arguments.
18
*args
If you do not know how many arguments that will be passed into
your function, add a * before the parameter name in the function
definition.
This way the function will receive a tuple of arguments, and can
access the items accordingly:
Output
19
**kwargs
If you do not know how many keyword arguments that will be
passed into your function, add two asterisk: ** before the parameter
name in the function definition.
This way the function will receive a dictionary of arguments, and
can access the items accordingly:
20
**kwargs
If you do not know how many arguments that will be passed into
your function, add a * before the parameter name in the function
definition.
This way the function will receive a tuple of arguments, and can
access the items accordingly:
21
Return Values
To let a function return a value, use the return statement
22
Return Values
Example 2
23
The Anonymous Function
These types of Python functions are anonymous since we do not
declare them, as we declare usual functions, using the def keyword.
We can use the lambda keyword to define the short, single output,
anonymous functions.
Lambda expressions can accept an unlimited number of arguments;
however, they only return one value as the result of the function.
They can't have numerous expressions or instructions in them. Since
lambda needs an expression, an anonymous function cannot be
directly called to print.
24
The Anonymous Function
Lambda functions contain their unique local domain, meaning they
can only reference variables in their argument list and the global
domain name.
Syntax
Lambda functions have exactly one line in their syntax:
lambda [argument1 [,argument2... .argumentn]] : expression
25
The Anonymous Function
Example:
26
Scope and lifetime of variables
The scope of a variable refers to the domain of a program wherever
it is declared. A function's arguments and variables are not
accessible outside the defined function. As a result, they only have a
local domain.
The period of a variable's existence in RAM is referred to as its
lifetime. Variables within a function have the same lifespan as the
function itself.
When we get out of the function, they are removed. As a result, a
function does not retain a variable's value from earlier executions.
27
Scope and lifetime of variables
Example:
28