The shape of the lesson
By the end of the lesson, learners can
- Procedures and CALL: In the lab, choose Procedure with a parameter and step through it.
- Functions return a value: A function works like a procedure, but it hands a single value back to the place where it was called.
- Local and global variables: The lab's Local and global variables program shows the same idea: the procedure has its own Total, so the global Total is not changed.
- Procedure: PROCEDURE ... ENDPROCEDURE, run with CALL, returns nothing.
- Function: FUNCTION ... RETURNS type ... RETURN value ... ENDFUNCTION, used in an expression.
- Parameters: name and data type in the header; values passed in the call, in the same order.
- Scope: local inside a subroutine only; global anywhere in the program.
One picture every idea returns to
A procedure is a named block of code that performs a task; it is run with CALL as a statement on its own and does not return a value. A function is a named block of code that returns a single value, so it is used inside an expression; its header states the data type it RETURNS. Parameters are the variables in the header that receive the values passed in when it is called (up to three in the exam). A local variable is declared inside a subroutine and can only be used there; a global variable is declared in the main program and can be used anywhere.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Write it once.. Large programs are built from small named blocks of code. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: Procedures and CALL | In the lab, choose Procedure with a parameter and step through it. Project tab 5 of the lab (Procedures, functions and the call stack) and let a student drive it while the class predicts. Model the worked example Two calls on the board, one line at a time. | Whole class, then pairs |
| 13 to 21 min | Teach and check: Functions return a value | A function works like a procedure, but it hands a single value back to the place where it was called. Model the worked example A function with IF on the board, one line at a time. | Pairs |
| 21 to 30 min | Teach and check: Local and global variables | The lab's Local and global variables program shows the same idea: the procedure has its own Total, so the global Total is not changed. Model the worked example A procedure that uses a global on the board, one line at a time. | Connect-Extend-Challenge |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Local and global variables, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Connect-Extend-Challenge
After the new idea, each student writes three lines: how it connects to what they knew, how it extends their thinking, and what still challenges them. Pairs compare, then the challenges become the plenary.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| A procedure with no parameters is still called with CALL, e.g. | CALL DrawLine. Do not write a procedure call inside an expression: it has no value to give back. |
| Never use CALL with a function. | Its value must be used: assigned, output or part of a calculation, e.g. Total ← SumSquare(2, 3). |
| The lab program outputs Local Total 6 and then Global Total 10: assigning to a local variable with the same name never touches the global one. | Return to Local and global variables: ask a pair to state the correct version and show it on the lab before moving on. |
Procedures, functions, parameters and scope: worksheet
6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.
Explain the difference between a procedure and a function, including how each is called.
Describe what is meant by a local variable and a global variable.
Write a function Average that takes three integer parameters and returns their mean as a real number.
State the output of this pseudocode.
FUNCTION Double(N : INTEGER) RETURNS INTEGER
RETURN N * 2
ENDFUNCTION
PROCEDURE Show(N : INTEGER)
OUTPUT "Value: ", N
ENDPROCEDURE
DECLARE X : INTEGER
X ← Double(3)
CALL Show(X + 1)
CALL Show(Double(X))Write a procedure Greet with a STRING parameter Name and an INTEGER parameter Times that outputs "Hello " followed by the name, Times times. Then write a statement that calls it for the name Sam, twice.
A student writes CALL Total ← Add(2, 3), where Add is a function. Explain what is wrong and correct it.
Answer key and marking notes
- A function returns a single value; a procedure does not return a value.
- A procedure is called with CALL as a statement on its own.
- A function is called as part of an expression, e.g. assigned to a variable or output.
- Local: declared inside a procedure or function and can only be used within it.
- Global: declared in the main program and can be used anywhere in the program.
FUNCTION Average(A : INTEGER, B : INTEGER, C : INTEGER)RETURNS REALat the end of the headerRETURN (A + B + C) / 3(brackets needed)ENDFUNCTION
- X ← Double(3) = 6.
- Show(6 + 1) outputs Value: 7.
- Double(6) = 12, so Show outputs Value: 12.
PROCEDURE Greet(Name : STRING, Times : INTEGER)FOR Count ← 1 TO Times,OUTPUT "Hello ", Name,NEXT Count,ENDPROCEDURECALL Greet("Sam", 2)
- CALL is only used for procedures; a function is used inside an expression.
- Correct:
Total ← Add(2, 3).
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
What is the main difference between a function and a procedure?
- AA function returns a single value to the place where it was called; a procedure does not
- BA function can have parameters; a procedure never has parameters
- CA procedure is pre-written by the language; a function is written by the programmer
- DA procedure can only be called once; a function can be called many times
FUNCTION Square(N : INTEGER) RETURNS INTEGER returns N * N. Which statement uses it correctly?
- A
Total ← Square(5) - B
CALL Square(5) - C
Square(5) ← Total - D
CALL Total ← Square(5)
What is output when the main program runs?
PROCEDURE Show(Start : INTEGER)
DECLARE Count : INTEGER
FOR Count ← Start TO 5
OUTPUT Count
NEXT Count
ENDPROCEDURE
CALL Show(3)
CALL Show(4)- A3 4 5 4 5
- B3 4
- C3 4 5
- D4 5 3 4 5
What is a local variable?
- AA variable declared inside a procedure or function that can only be used there
- BA variable declared in the main program that can be used anywhere in the program
- CThe value passed into a procedure in brackets when it is called
- DA variable whose value cannot be changed while the program is running
What is output?
FUNCTION Twice(N : INTEGER) RETURNS INTEGER
RETURN N * 2
ENDFUNCTION
OUTPUT Twice(Twice(3) + 1)- A14
- B7
- C16
- D13
What is output?
PROCEDURE Update
DECLARE Total : INTEGER
Total ← 5
Score ← Score + Total
ENDPROCEDURE
DECLARE Score : INTEGER
DECLARE Total : INTEGER
Score ← 10
Total ← 1
CALL Update
CALL Update
OUTPUT Score, " ", Total- A20 1
- B20 5
- C12 1
- D15 5
Quiz key
A function returns a value (its header says RETURNS and a data type), so it is used inside an expression; a procedure is run with CALL as a statement and returns nothing. Both can take parameters. Both can be written by the programmer, and library routines can be either. Both can be called as many times as needed.
Total ← Square(5) uses the returned value 25 by storing it. CALL is only for procedures and would throw the returned value away. A function call cannot be on the left of the arrow, because it is not a variable. CALL is never combined with an assignment.
CALL Show(3) passes 3 to Start, so it outputs 3, 4, 5. CALL Show(4) then outputs 4, 5. In order: 3 4 5 4 5. 3 4 outputs only the two argument values. 3 4 5 forgets the second call. 4 5 3 4 5 runs the calls in the wrong order.
A local variable is declared inside a subroutine and exists only there. A variable declared in the main program and usable everywhere is global. The value in brackets on a call is an argument, received by a parameter. A named value that cannot change is a constant.
Twice(3) = 6, so the argument of the outer call is 6 + 1 = 7, and Twice(7) = 14. 7 stops after the inner call and the addition. 16 adds the 1 before the first doubling: Twice(Twice(4)). 13 doubles twice and then adds 1: Twice(Twice(3)) + 1.
Inside Update, Total is a local variable set to 5, while Score is not declared there, so the global Score is changed: 10 + 5 + 5 = 20. The global Total is never touched, so it stays 1. 20 5 assumes the local Total overwrites the global one. 12 1 uses the global Total (1) in the addition. 15 5 adds only once and also overwrites the global Total.
What to project
The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.