05. Functions

Arindam BaidyaArindam Baidya
3 min read
>>> def input_number():
...      return int(input("Enter a number: "))
...
>>> input1 = input_number()
Enter a number: 25
>>> input1
25
  • Function must be define first, and then can call it.

Function - Arguments

  • Passing values to a function
>>> def input_number(num):
        return int(input("Enter a number: ")) * num
>>> input1 = input_number(10)
>>> input1
Enter a number: 12
>>> print(input1)
120

Function - parameters

Positional parameters

>>> def input_number(num1, num2):
        return int(input("Enter a number: ")) * (num1 - num2)
>>> input1 = input_number(10, 20) # num1: 10; num2: 20

Named parameter

>>> def input_number(num1, num2):
        return int(input("Enter a number: ")) * (num1 - num2)
>>> input1 = input_number(num2 = 20, num1 = 30) # Position doesn't matter here

Error

>>> def input_number(num1, num2):
        return int(input("Enter a number: ")) * (num1 - num2)
>>> input1 = input_number(20, num1 = 30)

Default values for parameters

>>> def input_number(num = 10):
        return int(input("Enter a number: ")) * num
>>> input_number()
Enter a number: 12
120

If we now pass a value, Python will ot use the default value. It will use the value that will be pass.

>>> def input_number(num = 10):
        return int(input("Enter a number: ")) * num
>>> input_number(5)
Enter a number: 12
60

Function - Return

Functioins don’t necessarily have to return any vaue, they can aso simply perform actions. For example, printing something to the console.

>>> def print_sum(num1, num2):
        sum = num1 + num2
        print("The sum is: ", str(sum))
>>> print_sum(10, 20)
The sum is: 30
>>> def print_sum(num1, num2):
        sum = num1 + num2
        if(sum == 0):
            return
        print("The sum is: ", str(sum))
>>> print_sum(10, 20)
The sum is: 30
>>> print_sum(1, -1)
>>>

None: Variable that doesn’t returns any value

>>> def is_even(num):
        if(num % 2 == 0):
            return True
>>> print(is_even(6))
True
>>> print(is_even(7))
None

List as an argument

>>> def multiply_values(list):
        multiply_values = []
        for item in list:
            multiply_values.append(item * 2)
        return multiply_values
>>> print(multiply_values([1, 2, 3]))
[2, 4, 6]
>>> print(multiply_values([-2, -4, -8]))
[-4, -8, -16]
>>> print(multiply_values(1))
TypeError

Scopes

Parameters and all values that are declared within the function body are scoped to just that function.

  • This means, variables that are decared within the function body are only available within that function itself, not outside of it.
>>> def input_number():
        result = int(input("Enter a number: ")) * 100
        return result
>>> print(result)
NameError: name 'result' is not defined

We can use variable inside the function, that are declared outside the function (or in global scope).

>>> num = 100
>>> def input_number():
        result = int(input("Enter a number: ")) * num
        return result

We can use same variable name in both global scope and function scope.

>>> num = 100
>>> def input_number():
        num = 50
        result = int(input("Enter a number: ")) * num
        return result

Here, function will use function scope ‘num’ varibale, as it is present. Otherwise it will use the global scope ‘num’.

If we want to access the varible declared in the function scope, we need to initialise it as a global variable using global keyword.

>>> num = 100
>>> def input_number():
        global own_num
        own_num = 60
        result = int(input("Enter a number: ")) * own_num
        return result
>>> input_number()
2
>>> print(own_num)
60

Arguments

>>> age = 22
>>> def multiply(num)
        num *= 2
        print("In multiply: ", str(num))
>>> multiply(age)
In multiply: 44
>>> print(age)
22
>>> nums = [1, 2, 3]
>>> def change_first_item(list):
        list[0] = 9
>>> change_first_item(nums)
>>> print(nums)
[9, 2, 3]
0
Subscribe to my newsletter

Read articles from Arindam Baidya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arindam Baidya
Arindam Baidya