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
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:
1 Sum is 800
2 None
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.
Leave a Reply