Chapter 6: Function calls

Return values

face Josiah Wang

The functions abs() and input() will each return a value. Or, to be more precise, they will return an object (which has a value).

You can then assign this object to a variable, if desired.

>>> x = abs(35)
>>> x
35
>>> message = input("Enter a message: ")
Enter a message: I love Python
>>> message
'I love Python'

Some functions do NOT return a value. The function print() is an example of this.

1
2
3
4
5
6
7
>>> x = print("hello")
hello
>>> x
>>> print(x)
None
>>> type(x)
<class 'NoneType'> 

You can see that x actually has no value (Line 3).

If you print out x (Line 4), you can see that the output is None (Line 5).

None is a special keyword in Python to represent “nothing”. So x points to “nothing”.

Remember that everything is an object in Python. This includes None. In Line 7, you can also see that None is a special object of type NoneType.

You can assign a variable to None, e.g. x = None

None is an object

You will see more examples of None as you progress further. Just remember that it is simply “an object that represents nothing”.

But… the prompt prints “hello” in Line 2!

Some functions produce what is known as side-effects.

This is when a function does something that affects the outside world, besides just doing some local computation and returning a value.

For example, a coffee machine might produce some sound or heat from the motors, but this is not the main output that it is meant to produce (the freshly brewed coffee is the real output).

In this example, print("hello") prints out the argument hello to the console. But it does NOT return a str 'hello' (notice the lack of quotes in Line 2).

Make sure you understand the difference, otherwise things may get confusing later.