One name for a group
The same three blocks appear again and again. Give them one name. Write the group once, with its name. That is defining it.
Step 1 of 4
- One group: dot, dot, new line.
- Four copies written out: 12 lines.
- Named: 4 lines to define it, then 4 calls: 8 lines.
- The blocks that run are the same: 12.
The rule
A named group of blocks is a function. Using its name is a call.
Naming a group does not change which blocks run. It changes how many lines you read.
| Copies | Written out | Named | Blocks run |
|---|---|---|---|
| 1 | 3 | 5 | 3 |
| 2 | 6 | 6 | 6 |
| 3 | 9 | 7 | 9 |
| 6 | 18 | 10 | 18 |
Here we count only the blocks that run. A call only points to the group. A call line is not counted as a block.
Naming costs 4 lines to define the group. That is 1 line for the name and 3 for the blocks. Each call adds 1 line. For 3 copies, naming takes 4 + 3 = 7 lines. Writing it out takes 3 × 3 = 9 lines. At 2 copies, both need 6 lines. From 3 copies, naming needs fewer lines.
Change the copies. Compare the lines.
Loading lab…