Robotics & Artificial Intelligence
Distinguish between void functions and non-void functions with the help of an example.
Answer
Void functions are those functions that do not return any value. They may or may not contain a return statement, but even if the return statement is present, it returns no value. When such a function is called, it automatically returns None. Void functions are also called non-fruitful functions.
Example of a void function:
def msg():
print("Welcome")
msg()
In this example, the function msg() only prints a message and does not return any value. Hence, it is a void (non-fruitful) function.
A non-void function is a function that returns a value using the return statement after execution. The value returned is passed back to the calling statement. Non-void functions are also known as fruitful functions.
Example of a non-void function:
def add(a, b):
return a + b
result = add(4, 6)
print(result)
In this example, the function add() returns the sum of two numbers using the return statement. Hence, it is a non-void (fruitful) function.