Defining Functions

Void Functions and Functions Returning Values

In Python, void functions are not exactly the same as functions you see in C, C++ or Java. Meaning to say, if the function body does not have any return statement then a special value None returns when the function terminates. This article will throw light on void functions in detail.

void functions

Void Functions

You will see that in Python, None refers to a literal kind of type NoneType which comes in use for denoting the absence of a value. Moreover, it is generally assigned to a variable for denoting that the variable does not point to any object.

Through the program given below, you can see how it shows that void functions return None:

                    

1 def add(num1, num2):

2 print("Sum is", num1 + num2)

3

4 return_val = add(300, 500)

5 print(return_val)

Output:

Moreover, add () function certainly returns None. Thus, one can say that in Python, all functions return value whether usage of the return statement is done or not. But, this does not imply that one can make use of void functions like a value-returning function.

Browse more Topics Under Defining Functions

Functions Returning Values

Most of the functions need arguments, values which control how the function will carry out its job. For instance, if one wishes to find the absolute value of a number, one must indicate what the number is. Further, Python has a built-in function for computing the absolute value:

                    

#

1 print(abs(4))

2

3 print(abs(-4))

4

In this instance, the arguments to the abs function are 4 and -4.

Moreover, some functions take up more than one argument. For instance, the math module comprises a function which we refer to as pow which takes two arguments, the base and the exponent.

                    

#

1 print(abs(4))

2

3 print(abs(-4))

4

In this instance, the arguments to the abs function are 4 and -4.

max can be sent any number of arguments, which separates by commas, thus, it will return the maximum value sent. Similarly, the arguments may either be simple values or expressions.

Moreover, in the last instance, you will see 503 is returned. Thus, it is larger than 33, 125, and 1. Please note that max also works on lists of values. In addition, functions like range, int, abs all return values that can come into use for building more complex expressions.

Therefore, you see that a significant difference between these functions and drawSquare is that the latter was not executed as it was required to compute a value. Alternatively, drawSquare was written for executing a sequence of steps that cause the turtle to draw a particular shape. Further, we sometimes refer to functions returning values as fruitful functions.

FAQ on Void Functions and Functions Returning Values

Question 1: Can Python functions return values?

Answer 1: Yes, a Python function will always have a return value. In other words, there is no notion of procedure or routine in Python. Thus, if one does not explicitly use a return value in a return statement, or if one totally omits the return statement, then Python will implicitly return a default value for oneself.

Question 2: How do you pass a value back to a function in Python?

Answer 2: In order to do that, one has to explicitly pass the return value into the second function. The function returns a value but it does not create the symbol in any kind of global namespace as your code assumes. One does not have to actually capture the return value in the calling scope and then use it for subsequent operations.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.