Functions

Returning values

The value returned by the function can be saved into a variable so that value can be used in other parts of the code.

In this example, we will just print() the value stored in the variable to the screen.

The return command in a function specifies what value should be sent back to the part of the program where the function was called. This allows the program to use that value for further processing. Without a return statement, a function will return None by default.

For example, the function hello_world() is defined to return the string 'hello, world!'. This string is the output of the function when it is called.

Run the following code
def hello_world():
# The return value for this function is the string hello, world!
return 'hello, world!'

print('Calling the function hello_world(), saving its return value to a variable, and outputting the variable to the screen')
output = hello_world()
print(output)
print('')