Use functions in Python

Business Benefits

Perform specific tasks and build them to be used repeatedly within the same code or call them externally from other Python code.

Define the function in Python using the header and body.

A function consists of two parts: header and body. The function header starts with the def keyword, followed by name of the function, followed by arguments and ends with a colon (:).


def function_name(arg1, arg2, arg3 ... argN):
    # function body


```For example, create function to greet a user. Call that function using just the function name:

def greet():
    print("Hello !")
    print("Today is",

>>> print greet()
>>> Hello!
>>> Today is March, 12, 2021
## Use local variables or global variables within the function to access variables inside the body of the function, or outside of any function.

For example, create a function with local and global variable:

global_var = 200 # a global variable

def func():

# local_var is a local variable
# and is only available inside func()
local_var = 100

print("Inside func() - local_var =", local_var)

# accessing a global variable inside a function
print("Inside func() - global_var =", global_var)


print(“Outside func() - global_var =”, global_var)

print(“Outside func() - local_var =”, local_var) # ERROR: local_var is not available here

Inside func() - local_var = 100
Inside func() - global_var = 200
Outside func() - global_var = 200

## Create a function with variables and pass arguments within the same code for the function.

A function can take any number of arguments or none at all. For example, `print()` function accepts one or more arguments but `random.random()` function accepts none.

For example, define the parameter variable in the function header which will receives an argument when the function is called.

Create the function add_100 with single argument:

def add_100(num):
print(“Result =”, num+100)

def add_100(num): # num is a parameter
print(“Result =”, num+100)

x = 100

Result = 200

## Use the pass by value method to pass the value of the argument to the parameter.

When a function is called with arguments, it is the address of the object stored in the argument is passed to the parameter variable. This mechanism is known as Pass By Value.

For example:

def func(para1):
print(“Address of para1:”, id(para1))

arg1 = 100
print(“Address of arg1:”, id(arg1))

Address of arg1: 1536218288
Address of para1: 1536218288

## Use positional and keyword arguments to pass arguments to a function the same way the arguments are defined for the function.

Using positional argument passes arguments to a function in the same order as their respective parameters in the function header.

For example:

def is_pythagorean_triplet(base, height, perpendicular):
if base ** 2 + height ** 2 == perpendicular ** 2:
print(“Numbers passed are Pythagorean Triplets”)
print(“Numbers passed are not Pythagorean Triplets”)

## Use the `return` method to return the value from the function.

Create a function and return value from it using the `return` statement. Its syntax is:

return [expression]

def add(num1, num2):
return num1 + num2

For example:

result = add(12, 10) * 10

if add(12, 10) == 100:
print(“It is True”)

print(add(12, 10))

## Create a function and return multiple values from it.

Specify each return value separated by a comma (`,`) after the `return` keyword to return multiple values.


return val1, val2, val3, …, valN

def sort_two_num(num1, num2):
if num1 < num2:
return num1, num2
return num2, num1

number1, number2 = sort_two_num(100, 15)
print(“number1 is”, number1)
print(“number2 is”, number2)

number1 is 15
number2 is 100

## Use the default arguments method to pass values based on the defined variables within the function.

Specify the default value for the parameter using the assignment operator followed by the parameter name.

For example:

def calc_area(length=2, width=3):
print(“length=”, length, ", width = ", width)
print(“Area of rectangle is”, width * length)

calc_area(4, 6)
calc_area(width=100, length=23)

length= 2 , width = 3
Area of rectangle is 6

length= 4 , width = 6
Area of rectangle is 24

length= 23 , width = 100
Area of rectangle is 2300

length= 12 , width = 3
Area of rectangle is 36

Last edited by @hesh_fekry 2023-11-14T11:07:38Z