Defining and calling functions¶
~20 min ยท prerequisite: Reading error messages, and the Part 3 lessons on strings, lists, and dictionaries
What you will be able to do¶
- Define a function with
def, give it parameters, and call it. - Send a result back to the caller with
return, and store that result in a variable. - Give a parameter a default value, and pass an argument by its name.
The idea¶
A function is a named block of code. You write it once, give it a name, and run it whenever you need it by calling it with parentheses. A function lets you name one step, such as "add tax to a price", and reuse that step instead of copying the same lines again and again.
Think of a recipe card. It has a name, it lists what you must hand over, and it says what you get back. A function works the same way.
You create a function with the keyword def, followed by the name, a pair of parentheses, and a colon. The lines that belong to the function are indented below the def line. Python does not run those lines when it reads the def. It runs them when someone calls the function.
The names inside the parentheses are parameters. They are placeholders that receive values. The values you pass in when you call the function are arguments. In square(4), the parameter is number in the definition, and 4 is the argument.
A function sends a value back with return. The return statement ends the function and gives the value to the place where the function was called. If a function has no return statement, the call gives back the special value None. The word None means that there is no value.
A parameter can have a default value, written with an equals sign in the definition, such as rate=0.2. If the caller leaves that argument out, Python uses the default. A caller can also pass an argument by its parameter name, such as rate=0.5. This is a keyword argument. Naming the argument makes the call easier to read.
Many functions begin with a short docstring, which is a string in triple quotes on the first line of the body. Python stores it with the function, and readers and tools use it to explain what the function does.
Worked example¶
def greet(name, punctuation="!"):
"""Return a greeting for one person."""
return "Hello, " + name + punctuation
message = greet("Ada")
print(message)
print(greet("Grace", punctuation="?"))
Expected output:
Line by line, this is what Python does:
def greet(name, punctuation="!"):creates a function namedgreet. It has two parameters, andpunctuationhas the default value"!". Nothing else runs yet.- The docstring is stored with the function. It is not printed.
return "Hello, " + name + punctuationjoins three strings and sends the result back to the caller.message = greet("Ada")calls the function.namereceives"Ada". No punctuation is given, sopunctuationuses"!". The returned text is stored inmessage.print(message)showsHello, Ada!.greet("Grace", punctuation="?")passes the second argument by name. The result isHello, Grace?, andprintshows it.
Try it¶
The first block squares numbers. The second block uses a default value and a keyword argument. The third block shows a function with no return.
Common mistakes¶
-
Forgetting the colon after
def. The following code has no colon at the end of the first line:Python reports:
Add
:at the end of thedefline. -
Forgetting to indent the body. The following code has a
returnline that is not indented:Python reports:
Indent the body by four spaces.
-
Using a function that has no
return. The following code adds 1 to the result of a call:Python reports:
The function computed
totalbut never returned it. Addreturn totalat the end of the body. -
Leaving out a required argument. The following call passes nothing to
greet:Python reports:
Pass the value the parameter needs, such as
greet("Ada").
Practice¶
- Define
double(number)that returns twice its argument. Assert thatdouble(7)equals14. - Define
full_name(first, last)that returns the two names joined by one space. Assert thatfull_name("Ada", "Lovelace")equals"Ada Lovelace". - Define
celsius_to_fahrenheit(celsius)that returnscelsius * 9 / 5 + 32. Assert that100gives212.0and0gives32.0. - Define
repeat_word(word, times=2)that returnswordrepeatedtimestimes. Assert thatrepeat_word("ha")equals"haha"and thatrepeat_word("ho", times=3)equals"hohoho". - Stretch: define
largest(numbers)that returns the largest number in a non-empty list, without callingmax. Assert thatlargest([3, 9, 4])equals9and thatlargest([-5, -2])equals-2.
Solution
- ```python def double(number): return number * 2
assert double(7) == 14 ```
- ```python def full_name(first, last): return first + " " + last
assert full_name("Ada", "Lovelace") == "Ada Lovelace" ```
- ```python def celsius_to_fahrenheit(celsius): return celsius * 9 / 5 + 32
assert celsius_to_fahrenheit(100) == 212.0 assert celsius_to_fahrenheit(0) == 32.0 ```
- ```python def repeat_word(word, times=2): return word * times
assert repeat_word("ha") == "haha" assert repeat_word("ho", times=3) == "hohoho" ```
- ```python def largest(numbers): best = numbers[0] for number in numbers: if number > best: best = number return best
assert largest([3, 9, 4]) == 9 assert largest([-5, -2]) == -2 ```
Recap¶
defcreates a function. Its body runs only when the function is called.returnsends a value back to the caller. A function withoutreturngives backNone.- Parameters can have default values, and any argument can be passed by its name.