Naming a job
After this: Write a block of lines with a name and run it by that name, as often as you like.
On this page · 7 sections · 3 checks
Picture a small bakery that prints a receipt for every customer. Every receipt starts with the same line: the shop's name. Nobody types that line again for each receipt. The till has a button called "header", and pressing it prints the line.
A program can have buttons like that. You write a few lines once, give them a name, and from then on you run them by writing the name. That is this whole tutorial. This lesson makes the first one.
A block with a name
A is a block of lines with a name. You write the block once. Each time you write the name with round brackets after it, the block's lines run. Writing the name like that is calling the function.
Here is the smallest useful example: a function named header with one print line, and a program that calls it twice.
- The first line gives the block its name,
header. The line under it, pushed in, is the block. The computer reads these lines and remembers them. It does not run them yet. - The next line is
header(): a call. The computer goes to the block, runs its print line, and comes back. - The line after that prints
Bread 2, as any print line would. - Then comes a second call,
header(). The block runs again, and the shop's name is printed a second time. - The last line prints
Milk 1.
Four lines are printed, and the shop's name was written in the program only once.
Watch it happen
Press Run it and step through. When the program reaches a call, something new appears in the Variables panel.
- Run the programRun it and step through. The first line only names the block. Watch the marker jump into the block each time the program reaches header(), and watch a frame appear and disappear in the Variables panel.
def header(): print("== Corner Bakery ==")
header()print("Bread 2")header()print("Milk 1")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
What appeared is a : the record of one call that has started and not finished. It carries the function's name and the boxes that belong to this call. While header runs, its frame is marked running and the program's own frame is marked waiting. When the block is done, the function returns: the computer goes back to the line that called it, and the frame is gone. The narration says "a new frame" at each call, and "returns … to line 47" when the block is done.
The code
def header(): print("== Corner Bakery ==")
header()print("Bread 2")header()print("Milk 1")== Corner Bakery == Bread 2 == Corner Bakery == Milk 1
public class Main { static void header() { System.out.println("== Corner Bakery =="); }
public static void main(String[] args) { header(); System.out.println("Bread 2"); header(); System.out.println("Milk 1"); }}== Corner Bakery == Bread 2 == Corner Bakery == Milk 1
Naming is not running
Writing the function does not run it. Only a call does. In the trace below the function is written but never called, so its line never prints, and the narration says that the program moves on past it.
def header(): print("== Corner Bakery ==")
print("Bread 2")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The rule: a function's lines run once for each call, and not at all without one.
Run it
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Add a third call to header at the end, and run it again.
Check yourself
A program writes a function header with one print line, and below it calls header() three times. How many times is that line printed?
What does this print?
def hello(): print("Hi")
hello()print("Bye")hello()public class Main { static void hello() { System.out.println("Hi"); }
public static void main(String[] args) { hello(); System.out.println("Bye"); hello(); }}Complete the first line so that the block is a function named greet that takes no inputs. Write the whole line, including the brackets and the colonbrackets and the opening brace.
____________________ # ← the first line: name the block greet print("Welcome")
greet()public class Main { ____________________ // ← the first line: name the block greet System.out.println("Welcome"); }
public static void main(String[] args) { greet(); }}Honesty notes
- Say what a function is: a block of lines with a name, written once and run by that name as often as you like.
- Write the first line of a function (def in Python, static void in Java) and the lines that belong to it.
- Call a function by writing its name with round brackets, and say that naming a function does not run it.
- Read a frame in the trace band: the record of one call, shown while the function runs and gone when it returns.