Parameters, Arguments and return
A function becomes really useful when you can hand it values and get a result back. Parameters are the names inside the function that receive what you hand in. return sends a result back out to whoever called the function. And confusing return with print is the single most common beginner mistake with functions, so this page makes it on purpose.
Think of a vending machine. You put in coins and press a button: those are the arguments. The machine hands you a drink: that is the return value. A machine that only showed you a picture of the drink on its screen would leave you thirsty, however clear the picture was.
Parameters and Arguments
In def to_fahrenheit(celsius):, the name in the brackets is a parameter. When you call to_fahrenheit(25), the value 25 is the argument, and for the length of that call the parameter celsius points at it. Parameter and argument are the two ends of the same handover: the name on the inside, the value from the outside. A function can have several parameters, separated by commas, and the arguments are matched to them in order.
return Hands a Result Back
return ends the function and hands a value back to the caller, where it can be stored, printed, or used in a calculation like any other value. Anything written after a return in the same block never runs.
def to_fahrenheit(celsius): return celsius * 9 / 5 + 32 warm = to_fahrenheit(25) print(warm) print(warm - 10)
77.0 67.0
print Is Not return
Here is the mistake. This version of the function prints its answer instead of returning it. The answer appears on the screen, so it looks as if it worked. But what the function hands back is None, the value every function returns when it has no return. Adding one to that fails, with a TypeError about NoneType.
def to_fahrenheit(celsius): print(celsius * 9 / 5 + 32) warm = to_fahrenheit(25) print(warm + 1)
77.0
Traceback (most recent call last):
File "/Users/you/python-course/print_not_return.py", line 5, in <module>
print(warm + 1)
~~~~~^~~
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Read the last line aloud: the plus sign cannot work with a NoneType and an int. NoneType is simply the type of None. Whenever you see None in an error you did not expect, suspect a function that printed its answer instead of returning it.
Defaults and Keyword Arguments
A parameter can have a default: a value it takes when the caller gives none. Write it with an equals sign in the def line. Callers can also name the parameter they are filling in, as a keyword argument, which makes the call read clearly and lets them skip the ones they are happy to leave as defaults.
def greet(name, greeting="Hello"): return f"{greeting}, {name}!" print(greet("Ada")) print(greet("Ada", greeting="Good morning"))
Hello, Ada! Good morning, Ada!
- "Printing inside the function returns the value." Printing only displays it. Without a return, the caller receives None.
- "Code after return still runs." return ends the function immediately. Lines after it in the same block never run.
- "Parameter names must match the names the caller uses." They are separate names. Only the order of the arguments, or the keyword you use, matters.
- Functions that return values can be combined, reused and, in Chapter 12, tested automatically. Functions that only print can do none of that.
- Reading "NoneType" as "something returned nothing" solves a whole family of confusing errors.
Knowledge Check
What does a function give back if it has no return line?
None, Python's value for no value at all- Whatever it printed last on the screen
- The value of its first parameter, unchanged
- An error, because every function needs a return
In def greet(name, greeting="Hello"):, what is "Hello"?
- The value that greet always returns to its caller
- A default used when no greeting is given
- A keyword that must be typed in every call to greet
- The name of the function that is being defined
The error says unsupported operand type(s) for +: 'NoneType' and 'int'. What is the likely cause?
- An int was added to a string somewhere in the line
- The plus sign cannot be used inside a function
- The program used a name before giving it a value
- A value that should be a number is None, often from a missing return
What happens to lines written after return in the same block?
- They run after the result has been handed back
- They run only when the function is called twice
- They never run, because return ends the function
- They run first, before the value is returned
You got correct