Four lines of your report print a label and an amount the same way: Total spent, Biggest spend, Smallest spend and Budget. This lesson writes that pattern once, as a function of your own, and uses it four times.
The concept
Here is your tracker as the last lesson left it. Run it:
Now run this:
Two report lines from one piece of code. The block starting with def creates a function called print_line. You have used functions since the first lesson: print, len, sum. Now you can write your own.
Each line at the bottom is a call. print_line("Total spent", 12500) runs the function's block with label set to "Total spent" and amount set to 12500. The second call runs the same block again with different values.
Defining a function and running it are two separate steps. Run this:
Only the line outside the function printed. def stores the steps under a name, and nothing in the block runs until you call it. To run it, you would write say_hello().
The syntax
def print_line(label, amount):
print(f"{label}: ₦{amount:,}")def, the function's name, parentheses, and a colon.- Inside the parentheses are the parameters: names the block can use. Separate them with commas.
- The block is indented four spaces, like every other block.
To call it, write its name and the values to use:
print_line("Budget", 15000)The values in a call are the arguments. They fill the parameters in order: the first argument goes into the first parameter, the second into the second.
Function names follow the same rules as variable names. A good one starts with a verb that says what it does: print_line, not line.
Define functions at the top of the file, so they exist before any line calls them. Python style leaves two blank lines after a function, to set it apart from the code below.
A minimal working example
def print_line(label, amount):
print(f"{label}: ₦{amount:,}")
print_line("Total spent", 12500)
print_line("Biggest spend", 4200)
print_line("Budget", 15000)Output: three report lines, all in the same format. To change that format, for example to add a full stop, you would change one line in the function, and every call would follow.
Common mistakes
You call the function before it is defined. Run this:
NameError: name 'print_line' is not definedPython runs top to bottom, and def is a line like any other. When the call on line 1 runs, the def below it has not run yet, so the name does not exist. Move the function to the top of the file.
You leave out an argument. Run this:
TypeError: print_line() missing 1 required positional argument: 'amount'The call must give one argument for each parameter. Python names the one that has no value. Give too many and you get the opposite: print_line() takes 2 positional arguments but 3 were given.
You leave off the parentheses. print_line on its own line gives no error, and does nothing. Without parentheses you are only naming the function, not calling it. A call always has parentheses, even when there are no arguments: say_hello().
Try it yourself
You will write print_line and use it in place of four report lines.
-
Run the starting code below. Find the four lines that print
Total spent,Biggest spend,Smallest spendandBudget. -
At the very top of the file, above
owner, add:def print_line(label, amount): print(f"{label}: ₦{amount:,}")That is the function, then two blank lines. Run it. Confirm nothing changed: defining a function prints nothing.
-
Replace your
Total spentprint withprint_line("Total spent", total). Run it. Confirm the line looks the same as before. -
Replace the other three in the same way:
print_line("Biggest spend", biggest),print_line("Smallest spend", smallest)andprint_line("Budget", budget). Run it. Confirm your whole report looks exactly as it did in step 1. -
Break it on purpose: change
print_line("Budget", budget)toprint_line("Budget"). Run it. Confirm the console shows aTypeErrorthat names the missing'amount'. -
Fix it: put
, budgetback. Run it. Confirm your report returns.
Your function prints a line, and that is all it does. Its result goes straight to the screen, so the rest of your program cannot use it. The next lesson writes a function that hands a value back.