Void Function in Python

Void function in Python is a function that performs an action but does not return any computed or final value to the caller.

It can accept any number of parameters and perform operation inside the function. A void function may or may not return a value to the caller.

If we do not declare return statement to return a value, Python will send a value None that represents nothingness.

The following example code demonstrates the void function, which will calculate the sum of two numbers and prints the result on the console.

Example 1:

# Python program demonstrates void function.
def voidOperation():
    num1 = int(input('Enter your first number: '))
    num2 = int(input('Enter your second number: '))
    sum = num1 + num2
    print('Sum of %d and %d = %d' %(num1, num2, sum))
def main_fn():
    voidOperation() # calling one function from another function.
main_fn() # calling main function.
Output:
      Enter your first number: 10
      Enter your second number: 50
      Sum of 10 and 50 = 60

Program explanation:

In the above program, we have declared a void function named voidOperation(), which does not return any computed value but performs basic operation inside it.


Inside the void function, we have taken two numbers as inputs from the user. Then, we have casted into int and stored them into two variables named num1 and num2, respectively.

When we have called the void function voidOperation() from inside another function main_fn(), the voidOperation() function performs the operation to calculate the sum of two numbers and prints the result on the console.

Example 2:

Let’s write a program in Python in which we will find even and odd numbers till n numbers using continue statement. We will take n numbers as input from the user.

# Python program to find even and odd numbers till n.
def EvenOddFunc(num):
    for i in range(1, num + 1):
        if i % 2 == 0:
            print('Even number: ',i)
            continue
        print('Odd number:',i)

def main_fun():
    number = int(input('Enter your number till you want to find an even or odd number: '))
    EvenOddFunc(number) # calling the function from another function with passing input.
main_fun() # calling main function.
Output:
      Enter your number till you want to find an even or odd number: 5
      Odd number: 1
      Even number:  2
      Odd number: 3
      Even number:  4
      Odd number: 5

More Advanced Example Programs

Example 3:

Let’s write a program in Python in which we will take a number from the user and check whether it is a palindrome.

A palindrome number is that number that when we read in reverse order is the same as is read in right order. For example, 121, 125521, 610016, etc.

# Python program to check whether a number is palindrome.
def checkPalindrome(n):
    String = ""
    if n.isdigit():
        length_of_string = len(n) # len() function finds the length of a string n.
        iNum = int(n) # converting string into integer.
        while iNum != 0:
            remDigit = iNum % 10
            String = String + str(remDigit)
            iNum = iNum // 10
        if n == String:
            print('Number is a palindrome.')
        else:
            print('Number is not a palindrome.')
def main_fn():
    N = input('Enter a number: ')
    checkPalindrome(N)
main_fn()
Output:
      Enter a number: 121
      Number is a palindrome.
      Enter a number: 123321
      Number is a palindrome.

Example 5:

Let’s write a program in Python in which we will accept a number from the user and check whether a number is a perfect number.


We will call a number as perfect number if it is equal to the sum of its factor other than the number itself. For example, 6 is a perfect number because 6 = 1 + 2 + 3.

# Python program to check whether a number is a perfect number.
def num_perfect(num):
    sum = 0
    for i in range(1, num):
        if num % i == 0:
            sum = sum + i
    if sum == num:
        print('Number is a perfect number.')
    else:
        print('Number is not a perfect number.')
def main_fn():
    N = int(input('Enter a number to check a perfect number: '))
    num_perfect(N)
main_fn()
Output:
      Enter a number to check a perfect number: 28
      Number is a perfect number.
      Enter a number to check a perfect number: 6
      Number is a perfect number.

Example 6:

Let’s write a Python program in which we will take marks of five subjects from a student as a user and calculate the total marks, percentage, and then display them on the console.

# Python program to calculate total marks and percentage of student's marks.
def marks(m1, m2, m3, m4, m5):
    total_marks = m1 + m2 + m3 + m4 + m5
    percentage = total_marks / 5
    print('Total marks obtained = ', total_marks)
    print('Percentage =', percentage)

# Main part of program.
def main_func():
    m1 = int(input('Enter your Science marks: '))
    m2 = int(input('Enter your Maths marks: '))
    m3 = int(input('Enter your English marks: '))
    m4 = int(input('Enter your Computer marks: '))
    m5 = int(input('Enter your Hindi marks: '))
    marks(m1, m2, m3, m4, m5)
main_func() # calling main function.
Output:
      Enter your Science marks: 89
      Enter your Maths marks: 90
      Enter your English marks: 96
      Enter your Computer marks: 89
      Enter your Hindi marks: 78
      Total marks obtained =  442
      Percentage = 88.4

In this tutorial, you have learned about void function in Python with the help of various example programs. I hope that you will have understood the basic points of void function and practiced all example programs. In the next, you will learn scope in Python.
Thanks for reading!!!