Previous Index Next

Introduction to Functions

A function in Python is a block of statements that has a name and can be executed by calling it from some other place in your program. They allow you to divide a complex program into smaller, more manageable pieces, which makes the code easier to understand, maintain, and debug.

One of the key advantages of using functions is that they enable collaboration among multiple programmers. Different individuals can work on different functions simultaneously, making it easier to divide the work and maintain a large codebase.

Additionally, functions promote code reusability. If you have a task that needs to be performed in multiple places within a program or even in different programs, you can write that functionality once as a function and then reuse it whenever needed. This saves time and effort, as you don't have to rewrite the same code multiple times.

Defining and Calling a Function

In Python a function is defined using the def keyword. To call a function, use the function name followed by parenthesis. Example:

def message():
    print('Hello, how are you?')
    print('I am function.')

message()

Output

Hello, how are you?
I am function.

Here, is the flow of execution of above program

The above program has only one function, but it is possible to define many functions in a program. Following Program shows an example of a program with two functions: main and message.

def main():
    print('Inside main')
    message()
    print('Inside main again')

def message():
    print('Hello, how are you?')
    print('I am function.')

main()

Output

Inside main
Hello, how are you?
I am function.
Inside main again

Here, is the flow of execution of above program

function example

Passing Arguments to Functions

Data can be passed into functions as arguments. Arguments are specified inside the parentheses as shown in the following example:

def carea(radius):
    area = 3.14*radius*radius
    print('Area of circle is', area)

a = 5
carea(a)

Output

Area of circle is 78.5

Let's take a detailed look at the process by discussing it step by step:

  1. The variable a is defined and assigned the value 5 outside the function.
  2. When the function carea(a) is called, the value of a (which is 5) is passed as an argument to the function.
  3. Inside the function definition, the parameter radius receives the value of the argument passed to the function. In this case, the value of radius becomes 5.
  4. The function then performs the necessary calculations using the value of radius (which is 5) to calculate the area of the circle.

The variable which supply the values to a calling function is called actual parameter. The variable which receives the value from the called statement is termed as formal parameter.

Here, in this example:

radius is the formal parameter, which acts as a placeholder in the function definition. a (with a value of 5) is the actual parameter, which is passed as an argument when calling the function.

in Python, arguments are passed by object reference. In this scenario, the argument a is passed by object reference. It means that the reference to the object that a points to (the integer object with the value 5) is passed to the radius parameter. Both a and radius reference the same object in memory.

two functions

Modifying Argument Inside Function

All arguments in the Python are passed by object reference. If you pass immutable arguments like integers, strings or tuples to a function, they can't be changed within the function because they are immutable.

Program (passing-string.py)

def change(yourtext):
    yourtext = 'I am updated text'

mytext = 'I am original text'
change(mytext)
print(mytext)

Output

I am original text

If you pass mutable arguments like lists and dictionaries to a function, they can be changed within the function. But you have to consider two cases:

When a list / dictionary is passed as an argument and the elements of a list changed inside functions, list / dictionary will be changed in the caller's scope.

When a new list / dictionary is assigned to the name, the old list / dictionary will not be affected.

Program (passing-list.py)

def change(yourlist):
    yourlist[0] = 'mango'
    yourlist[1] = 'orange'

def create_new(yourlist):
    yourlist = ['mango','orange']

mylist = ['apple','banana']
print(mylist)

create_new(mylist)
print(mylist)

change(mylist)
print(mylist)

Output

['apple', 'banana']
['apple', 'banana']
['mango', 'orange']

Default Arguments / Default Parameter Values

When one or more top-level parameters have the form parameter = expression, the function is said to have 'default parameter values.'

For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter's default value is substituted.

If a parameter has a default value, all subsequent parameters must also have a default value. In the following example, the rate parameter has a default value; therefore, the time parameter must also have a default value.

def simple_interest(principle, rate = 8, time = 1):
    si = principle * rate * time / 100
    print(si)

simple_interest(1200)
simple_interest(1200,11)
simple_interest(1200,11,4)

Output

96.0
132.0
528.0

Previous Index Next