Why functions
Problem
One change, three places
You are writing the till program for a small shop. Every receipt starts with the shop's name and phone number, so you have copied those two lines everywhere a receipt is printed.
The shop changes its phone number. You fix it in two places and miss the third:
print("Corner Shop")print("tel. 02 123 4567")print("Bread €1.20")
print("Corner Shop")print("tel. 02 123 4567")print("Milk €2.10")
print("Corner Shop")print("tel. 02 765 4321")print("Coffee €3.50")Corner Shoptel. 02 123 4567Bread €1.20Corner Shoptel. 02 123 4567Milk €2.10Corner Shoptel. 02 765 4321Coffee €3.50The third customer gets a receipt with the old number. The more copies there are, the easier it is to miss one.
The idea
Write it once, call it by name
Put the repeated part in one place and give it a name. That is a function: a named block of code that does one job.
You write the function once. Then, wherever you need it, you write just its name with parentheses: print_header(). This is called calling the function.
If the shop changes its phone number again, you fix one line in the function, and every receipt is right.
See it
What happens on a call
The program runs line by line. When it reaches a call, it goes into the function, runs its body and comes back to just after the call. Follow the ball: it is the point the program has reached.
def print_header(): print("Corner Shop") print("tel. 02 123 4567")
print_header()print("Bread €1.20")print_header()print("Milk €2.10")Step by step, in words
- def only stores the function. The first line that does something is print_header().
- A call: execution goes into the function.
- The function prints the top of the receipt.
- The indented lines are over, so the function is done: execution goes back to just after the call.
- The program carries on: it prints the bread.
- A second call: the same code, without writing it again.
- The top is printed again.
- Back in the program, on to the next line.
The second time, you do not write the top part again. You call the same function, and it prints the same two lines.
The idea
How to write a function
Here is the whole program, so you can see where the function goes:
def print_header(): print("Corner Shop") print("tel. 02 123 4567")
print_header()print("Bread €1.20")Corner Shoptel. 02 123 4567Bread €1.20def(short for define) says a function follows. The line only stores it under a name; it does not run it.- The name says what the function does, so it is a verb:
print_header,print_footer. Python names are lowercase, with words joined by underscores. - The empty parentheses mean the function takes nothing from outside. We will fill them in the next lesson.
- The body is the lines indented by 4 spaces after the colon. The first line that is not indented is no longer part of the function.
- The function sits above the lines that call it. Python reads the file from top to bottom and must have seen it already.
- There are two blank lines around the function. The program works without them, but this is how Python is written (the PEP 8 style guide).
Try it
In what order?
What will the program print?
def print_thanks(): print("Thank you!")
print("Order #1024")print_thanks()print("Order #1025")print_thanks()A function runs when it is called, not where it is written. Here it is called twice, so "Thank you!" is printed twice, each time after its order.
Try it
What is missing?
This function prints the bottom of the receipt. What goes in the blank?
print_footer():
print("We hope to see you again soon.")def: this is how every function starts.Try it
Find the bug
Pick the line with the bug.
print_thanks(). Without them you only mention the function, and it does not run. There is no error, which makes this mistake hard to catch: watch the output.Exercise
The receipt and the triangle
Solve these in your editor. Each one has sample output to compare yours with.
1. Receipt. Write three functions:
print_line()prints a line of 20 dashes with a loop;print_header()prints the shop's name, the phone number and the line;print_footer()prints the line and "Thank you!".
Below them, print two receipts: the top, one line with a purchase, the bottom.
Corner Shoptel. 02 123 4567--------------------Bread €1.20--------------------Thank you!Corner Shoptel. 02 123 4567--------------------Milk €2.10--------------------Thank you!2. Triangle. Write a function print_triangle() that uses nested loops to print a triangle of stars, 4 rows high:
* *** ************In the next lesson the height becomes a parameter: one function for a triangle of any height you like.
Test