Unit 5 (PY)
Unit 5 (PY)
Unit 5 (PY)
Functions can be both built-in or user-defined. It helps the program to be concise, non-
repetitive, and organized.
Syntax:
def function_name(parameters):
"""docstring"""
statement(s)
return expression
Creating a Function
We can create a Python function using the def keyword.
def fun():
print("Welcome to GFG")
Calling a Function
After creating a function we can call it by using the name of the function followed by parenthesis
containing parameters of that particular function.
def fun():
print("Welcome to GFG")
def evenOdd(x):
if (x % 2 == 0):
print("even")
else:
print("odd")
evenOdd(2)
evenOdd(3)
Output
even
odd
Types of Arguments
Python supports various types of arguments that can be passed at the time of the function call.
Let’s discuss each type in detail.
Default arguments
A default argument is a parameter that assumes a default value if a value is not provided in the
function call for that argument. The following example illustrates Default arguments.
print("x: ", x)
print("y: ", y)
myFun(10)
Output
('x: ', 10)
('y: ', 50)
Like C++ default arguments, any number of arguments in a function can have a default value.
But once we have a default argument, all the arguments to its right must also have default
values.
Keyword arguments
The idea is to allow the caller to specify the argument name with values so that caller does not
need to remember the order of parameters.
print(firstname, lastname)
# Keyword arguments
student(firstname='Geeks', lastname='Practice')
student(lastname='Practice', firstname='Geeks')
Output
('Geeks', 'Practice')
('Geeks', 'Practice')
Variable-length arguments
In Python, we can pass a variable number of arguments to a function using special symbols.
There are two special symbols:
def myFun(*argv):
print(arg)
def myFun(**kwargs):
# Driver code
Syntax: print(function_name.__doc__)
Example: Adding Docstring to the function
# A simple Python function to check
# whether x is even or odd
def evenOdd(x):
if (x % 2 == 0):
print("even")
else:
print("odd")
print(evenOdd.__doc__)
Output
Function to check if the number is even or odd
The return statement
The function return statement is used to exit from a function and go back to the function caller
and return the specified value or data item to the caller.
def square_value(num):
return num**2
print(square_value(2))
print(square_value(-4))
Output:
4
16
Is Python Function Pass by Reference or pass by value?
One important thing to note is, in Python every variable name is a reference. When we pass a
variable to a function, a new reference to the object is created. Parameter passing in Python is
the same as reference passing in Java.
Example:
def myFun(x):
x[0] = 20
print(lst)
Output
[20, 11, 12, 13, 14, 15]
When we pass a reference and change the received reference to something else, the
connection between the passed and received parameter is broken. For example, consider the
below program.
def myFun(x):
# After below line link of x with previous
# to x.
print(lst)
Output
[10, 11, 12, 13, 14, 15]
Another example to demonstrate that the reference link is broken if we assign a new value
(inside the function).
def myFun(x):
# to x.
x = 20
x = 10
myFun(x)
print(x)
Output
10