What does a function return if there is no return statement?

The python return statement is used to return values from the function. We can use the return statement in a function only. It can’t be used outside of a Python function.

Python Function without return statement

Every function in Python returns something. If the function doesn’t have any return statement, then it returns None.

def print_something(s):
    print('Printing::', s)


output = print_something('Hi')

print(f'A function without return statement returns {output}')

Output:

What does a function return if there is no return statement?
Python Function Without Return Statement

Python Return Statement Example

We can perform some operation in a function and return the result to the caller using the return statement.

def add(x, y):
    result = x + y
    return result


output = add(5, 4)
print(f'Output of add(5, 4) function is {output}')

Output:

What does a function return if there is no return statement?
Python Return Example

Python return statement with expression

We can have expressions also in the return statement. In that case, the expression is evaluated and the result is returned.

def add(x, y):
    return x + y


output = add(5, 4)
print(f'Output of add(5, 4) function is {output}')

Output:

What does a function return if there is no return statement?
Python Return Statement With Expression

Python return boolean

Let’s look at an example where we will return the boolean value of the argument of a function. We will use bool() function to get the boolean value of the object.

def bool_value(x):
    return bool(x)


print(f'Boolean value returned by bool_value(False) is {bool_value(False)}')
print(f'Boolean value returned by bool_value(True) is {bool_value(True)}')
print(f'Boolean value returned by bool_value("Python") is {bool_value("Python")}')

Output:

What does a function return if there is no return statement?
Python Return Boolean

Python return string

Let’s look at an example where our function will return the string representation of the argument. We can use the str() function to get the string representation of an object.

def str_value(s):
    return str(s)


print(f'String value returned by str_value(False) is {str_value(False)}')
print(f'String value returned by str_value(True) is {str_value(True)}')
print(f'String value returned by str_value(10) is {str_value(10)}')

Output:

What does a function return if there is no return statement?
Python Return String

Python return tuple

Sometimes we want to convert a number of variables into a tuple. Let’s see how to write a function to return a tuple from a variable number of arguments.

def create_tuple(*args):
    my_list = []
    for arg in args:
        my_list.append(arg * 10)
    return tuple(my_list)


t = create_tuple(1, 2, 3)
print(f'Tuple returned by create_tuple(1,2,3) is {t}')

Output:

What does a function return if there is no return statement?
Python Function Return Tuple

Further Reading: Python *args and **kwargs

Python function returning another function

We can return a function also from the return statement. This is similar to Currying, which is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.

def get_cuboid_volume(h):
    def volume(l, b):
        return l * b * h

    return volume


volume_height_10 = get_cuboid_volume(10)
cuboid_volume = volume_height_10(5, 4)
print(f'Cuboid(5, 4, 10) volume is {cuboid_volume}')

cuboid_volume = volume_height_10(2, 4)
print(f'Cuboid(2, 4, 10) volume is {cuboid_volume}')

Output:

What does a function return if there is no return statement?
Python Return Function

Python function returning outer function

We can also return a function that is defined outside of the function with return statement.

def outer(x):
    return x * 10


def my_func():
    return outer


output_function = my_func()
print(type(output_function))

output = output_function(5)
print(f'Output is {output}')

Output:

What does a function return if there is no return statement?
Python Function Return Outer Function

Python return multiple values

If you want to return multiple values from a function, you can return tuple, list, or dictionary object as per your requirement. However, if you have to return a huge number of values then using sequence is too much resource hogging operation. We can use yield, in this case, to return multiple values one by one.

def multiply_by_five(*args):
    for arg in args:
        yield arg * 5


a = multiply_by_five(4, 5, 6, 8)

print(a)
# showing the values
for i in a:
    print(i)

Output:

What does a function return if there is no return statement?
Python Return vs Yield

Summary

The python return statement is used to return the output from a function. We learned that we can also return a function from another function. Also, expressions are evaluated and then the result is returned from the function.

You can checkout complete python script and more Python examples from our GitHub Repository.

What does a function return without return?

Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.

What happens if a method does not have a return statement?

You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.

Can you have a function without a return?

A function without a return statement will return a default value. In the case of a constructor called with the new keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined .

What does it mean for a function to return None?

00:19 There are basically three ways to cause a value of None to be returned from a function: if the function doesn't have a return statement at all, if you have a return statement with no return value, or you can explicitly return None .