Chapter -3
Working with Functions
A function is a block of code that only executes when called. We can supply
parameters—data—to a function. As a result, a function may return data. You
can define function using def keyword.
Syntax for defining function –
def <function name> (<parameters>) :
<statement 1>
<statement 2>
.
.
.
Python Function Declaration
The syntax to declare a function is:
def function_name(arguments):
# function body
return
Here,
def - keyword used to declare a function
function_name - any name given to the function
arguments - any value passed to function
return (optional) - returns value from a function
Calling a Function in Python
In the above example, we have declared a function named greet().
def greet():
print('Hello World!')
Now, to use this function, we need to call it.
Here's how we can call the greet() function in Python.
# call the function
greet()
Example: Python Function
def greet():
print('Hello World!')
# call the function
greet()
print('Outside function')
Run Code
Output
Hello World!
Outside function
In the above example, we have created a function named greet().
Here's how the program works:
Here,
When the function is called, the control of the program goes to the function
definition.
All codes inside the function are executed.
The control of the program jumps to the next statement after the function call.
Flow of execution in a function call
The flow of execution refers to the order in which statements are executed during
a program run. The program’s opening statement is where execution always
starts. The statements are carried out one at a time, in ascending order. Although
the order in which a programme runs is unaffected by function declarations, keep
in mind that statements inside a function are not performed until the function is
called.
Example –
Defining function
def sum(x, y) :
Calling function
sum(a,b)
Where a, b are the values being passed to the function sum().
Passing parameters in function
Information that is supplied into a function is referred to as a parameter or
argument. The terms parameter and argument can both be used to refer to the
same thing. The function name is followed by parenthesis that list the arguments.
Simply separate each argument with a comma to add as many as you like.
Python supports three types of formal arguments/parameters
1. Positional arguments – Arguments that must be presented in the right order
are known as positional arguments. When calling a function, the first positional
argument must always be listed first and the second will be in second.
2. Default arguments – If no argument value is given during the function call,
default values will be passed in the function.
3. Keyword arguments – When values are supplied into a function, they are
known as keyword arguments . The assignment operator, =, and a parameter
come before a keyword argument.
Using multiple argument types together
Python allows you to combine multiple argument types in a function call.
Consider the following function call statement that is using both positional
arguments and keyword arguments.
interest(5000, time = 5)
Rules for combining all three types of arguments
An argument list must first contain positional arguments followed by any
keyword argument.
Keyword arguments should be taken from the required arguments
preferably.
You cannot specify a value for an argument more than once.
Returning values from functions
Functions in Python may or may not return a value. You already known about it.
There can be broadly two types of functions in python.
a. Function returning some value (non-void functions)
b. Function not returning any value (void functions)
Composition
A concept that represents a relationship is composition. Composition in general
refers to using an expression as part of a larger expression; or a statement as a
part of larger statement. In functions context, we can understand composition as
follows –
Example
An arithmetic expression
greater((4+5), (3+4))
A logical expression
test(a or b)
A function call (function composition)
int(str(52))
int(float(―52.5‖)*2)
int(str(52) + str(10))
Scope of Variable
The rules that determine which areas of the programme a specific piece of code
or data item would be known as and be accessible within are known as a
language’s scope rules.
There are two types of scopes in Python.
1. Global Scope
2. Local Scope
Global Scope – A name defined in a program’s top-level (_main) segment is
said to have a global scope and be usable throughout the entire programme and
all blocks contained therein.
Local Scope – Local scope describes a name that is declared in the body of a
function. It can only be utilized in this function and the other blocks that are
placed beneath it. The formal arguments’ names also have a local scope.
Mutable/Immutable Properties of Passed Data Objects
Type of Function
There are two types of function in python.
1. User – Define Function
2. Built – in – Function
The Python language includes built-in functions such as dir, len, and abs. The
def keyword is used to create functions that are user specified.
User – Define Function
User-defined functions are the fundamental building block of any programme and
are essential for modularity and code reuse because they allow programmers to
write their own function with function name that the computer can use.
Creating User Defined Function
A function definition begins with def (short for define). The syntax for creating a
user defined function is as follows –
Syntax –
def function_name(parameter1, parameter2, …) :
statement_1
statement_2
statement_3
….
The items enclosed in ―[ ]‖ are called parameters and they are optional.
Hence, a function may or may not have parameters. Also, a function may
or may not return a value.
Function header always ends with a colon (:).
Function name should be unique. Rules for naming identifiers also applies
for function naming.
The statements outside the function indentation are not considered as part
of the function.