Study Guide Instructor view

Procedures and Functions

Teaching notes for Lesson 5.9, with controls for presenting it.

Keep this page on your own screen. Share only the lesson window. The lesson window holds no teaching notes, so sharing it cannot show them.

Presenting

  1. Open the lesson in its own window and share that window, not this one.
  2. Use Next and Previous here, or the lesson’s own, to move through the lesson. Next reveals one thing at a time: the next idea, or the answer to the question on screen, then the next step. Previous takes the last reveal back. The line below says what is on screen and what Next will show.
  3. Reveal hints and answers when the class is ready, and reset a demonstration before using it again.

The controls need JavaScript and a browser that supports BroadcastChannel. Without them, open the lesson in presentation mode and use its own controls.

The two windows talk to each other only inside this browser, on this device. Nothing is sent to a server, and no account is needed. A second device (a phone or another tablet) cannot follow the lesson window yet.

Lesson plan

Level
Intermediate
Time
About 90 minutes; the steps below add up to 90

Pupils start from the pain of one line of dashes typed out thirty times and invent the idea of naming it, before PROCEDURE and CALL are met. They then add parameters, functions that return values, local and global variables, library routines (DIV, MOD, ROUND, RANDOM) and the habits that make a program maintainable. The recurring teaching move is to trace on the board where control goes (into the subroutine and back) and what each variable holds at each moment.

Objectives

  • Explain what a procedure and a function are, and how they differ
  • Define and call procedures and functions, with and without parameters (up to three), using CALL, RETURN and RETURNS correctly
  • Use a function call as part of an expression
  • Explain the difference between a local and a global variable, and say which parts of a program can use each
  • Use the library routines MOD, DIV, ROUND and RANDOM
  • Explain how subroutines, meaningful names and comments make a program easier to maintain

Before the lesson

  • Mini whiteboards, one per pupil, for predictions.
  • Write the DrawLine, Welcome, ShowTicket, TicketCost, AddBonus and Larger programs on the board or slides before the lesson so that you can trace them with the pen.
  • Use two colours: one for the main program and one for the lines inside a subroutine, so pupils can see control jump in and return.
  • Optional: index cards labelled with parameter names and argument values, so that pupils can physically hand a value to a procedure.

Steps

  1. Introduction Opening

    On screen: the lesson’s title, module and number; Next adds its objectives and key terms, as the lesson page lists them; Next again begins the first step.

    Objectives on screen

    • Explain what a procedure and a function are, and how they differ
    • Define and call procedures and functions, with and without parameters (up to three), using CALL, RETURN and RETURNS correctly
    • Use a function call as part of an expression
    • Explain the difference between a local and a global variable, and say which parts of a program can use each
    • Use the library routines MOD, DIV, ROUND and RANDOM
    • Explain how subroutines, meaningful names and comments make a program easier to maintain

    Purpose: Pupils start from the pain of one line of dashes typed out thirty times and invent the idea of naming it, before PROCEDURE and CALL are met. They then add parameters, functions that return values, local and global variables, library routines (DIV, MOD, ROUND, RANDOM) and the habits that make a program maintainable. The recurring teaching move is to trace on the board where control goes (into the subroutine and back) and what each variable holds at each moment.

  2. 1. The same lines, again and again Think 5 min

    Purpose: Pupils feel the cost of repeating the same line and propose naming it, before any keyword is given.

    Ask

    • What is the problem when the line of dashes appears in thirty places and the head teacher wants stars?
    • How could the program be arranged so that the change is made once?

    Responses to expect

    • Thirty edits and a risk of missing one
    • Write the line once and give it a name

    Misconceptions to challenge

    • A learner may believe: “Using find and replace solves the problem, so no new idea is needed.” Respond: It might for dashes, but ask what happens if the line is a block of ten instructions that differ slightly. A named block solves it at the root.

    Checking understanding: Pupils write the idea on a mini whiteboard: write it once and give it a name.

    Depending on the answers

    • If pupils only describe copy and paste: ask what happens when the original is changed, and whether the copies change too.

    English as an additional language

    • Check notice, framed and dashes; show the notices on a slide.

    Discussion pause: Thirty seconds of silent thinking, then pairs say the idea in one sentence.

    In the lesson window

    • Think it through: The head teacher wants the line to be made of stars instead of dashes, and the real program has the line in thirty places. What is the problem? How could the program be arranged so that the change is made only once?
  3. 2. Naming a set of instructions Discover 8 min

    Purpose: Pupils meet PROCEDURE and CALL through the DrawLine program, trace the order of the output, and say what happens at a CALL.

    Ask

    • What is output, in order, when this program runs?
    • What happens when CALL DrawLine is carried out?
    • What happens to the lines inside the definition when the program reaches them without a call?

    Responses to expect

    • Dashes, Spring Concert, dashes, Tickets: 40, dashes
    • The procedure's lines run and then the program carries on after the CALL
    • Nothing: a definition does not run

    Misconceptions to challenge

    • A learner may believe: “A procedure runs where it is defined.” Respond: A definition only describes it. The program starts at the first line outside a definition.
    • A learner may believe: “The program stops after a CALL.” Respond: Trace the output: after the first call comes Spring Concert, so control returned.
    • A learner may believe: “Each procedure can be called only once.” Respond: Each CALL runs it again, as the three dashes in the trace show.

    On the board or with the pen

    • Write the main program in one colour and the procedure in another; draw an arrow from each CALL into the procedure and back to the line after it.

    What learners should notice

    • The line of dashes is typed only once; changing it changes all three places.

    Checking understanding: The procedure-call question: the wrong options say the program stops, or that the lines already ran at the definition; each is rebutted by the trace.

    Depending on the answers

    • If pupils think a definition runs: ask them to trace which line is carried out first and why it is not the first line of the page.
    • If pupils are secure: ask them to change the dashes to stars and say how many lines of the program change.

    Support

    • Provide the trace table with the first row filled in.

    English as an additional language

    • Check define and call as to describe and to use; show a recipe card and a cooking step that says make the sauce.

    In the lesson window

    • Check your understanding: What happens when the statement CALL DrawLine is carried out?
  4. 3. Giving a procedure something to work with Predict 11 min

    Purpose: Pupils say what must differ between calls of a Welcome procedure, then meet parameters and arguments and trace a three-parameter procedure.

    Ask

    • What must be different from one call to the next, and how could the procedure get it?
    • What does each parameter hold for the two ShowTicket calls?
    • What goes wrong with CALL ShowTicket(12, 4, "Concert")?

    Responses to expect

    • The name, handed over at the call
    • Event Concert, Row 4, Seat 12; then Quiz, 1, 3
    • 12 goes to Event, which needs a STRING, and Concert to Seat, which needs an INTEGER

    Misconceptions to challenge

    • A learner may believe: “Arguments are matched to parameters by name.” Respond: The call contains no names, only values; they are matched by position.
    • A learner may believe: “Parameter and argument are the same.” Respond: The parameter is in the definition; the argument is the value in the call.
    • A learner may believe: “The procedure ignores the third value.” Respond: All three arguments are used; each goes to a parameter in order.
    • A learner may believe: “The argument must have the same name as the parameter.” Respond: Names need not match; the argument goes by its place in the list.

    On the board or with the pen

    • Write the definition and two calls; draw arrows from each argument to the parameter in the same position, then redraw with the wrong order and mark the type clash.

    Checking understanding: The argument-order question: the by-name and ignored-argument options come from the idea that matching is by name or by number of values.

    Depending on the answers

    • If pupils choose by name: ask how the computer would know which value is for Seat in the call, and what a call has in it.
    • If pupils are secure: ask for a call that would be accepted with the arguments in a different order and say what it would show.

    Support

    • Give pupils a card with the definition above the call and ask them to draw arrows.

    Stretch

    • Why does the syllabus limit a procedure to three parameters in this lesson, and what could you do with more values?

    English as an additional language

    • Check parameter, argument and match; hand over a card to show the value being given.

    In the lesson window

    • Think it through: A Welcome procedure is meant to show a different name each time it is used: “Welcome, Ravi”, then “Welcome, Chloe”. The lines inside it cannot be different each time. What must be different from one call to the next, and how could the procedure get it?
    • Check your understanding: A programmer writes CALL ShowTicket(12, 4, “Concert”) using the ShowTicket procedure above. What goes wrong?
  5. 4. A subroutine that gives back an answer Discover 13 min

    Purpose: Pupils discover that OUTPUT cannot give a value back, then meet functions with RETURNS and RETURN, trace the booking total, tell procedures from functions, and read the Larger example.

    Ask

    • Could the program add the 18 to the cost of 2 tickets if a procedure only outputs it?
    • What is Total after Total ← TicketCost(3) + TicketCost(2)?
    • Which line correctly uses the TicketCost function?
    • What does Larger(Larger(4, 9), 6) return?

    Responses to expect

    • No: OUTPUT shows a value but does not hand it back
    • 30, from 18 and 12
    • OUTPUT "Cost: ", TicketCost(4)
    • 9

    Misconceptions to challenge

    • A learner may believe: “A function is called with CALL.” Respond: A function call is part of an expression: no CALL.
    • A learner may believe: “TicketCost(4) on a line by itself works.” Respond: The value would go nowhere; it must be used in an expression.
    • A learner may believe: “OUTPUT inside a function gives the value back.” Respond: OUTPUT only shows it; RETURN hands it back.
    • A learner may believe: “A procedure can be used inside an expression.” Respond: A procedure does not hand back a value, so it cannot.

    On the board or with the pen

    • Trace TicketCost(3) + TicketCost(2) by writing the calls, crossing each out and writing the returned value over it: 18 + 12 = 30. Do the same for Larger(Larger(4, 9), 6), working the inner call first.

    Follow-up questions

    • Which would you use to check whether a password is valid: a procedure or a function, and why?

    Checking understanding: The which-call question: a call with CALL, a call as a bare line and a call inside an output; the wrong options are the two most common function-call slips.

    Depending on the answers

    • If pupils choose CALL: ask where the returned value would go if CALL were used.
    • If pupils are secure: ask them to write a function that returns the cost of a number of ice creams at 2 pounds each.

    Support

    • Provide the trace table with the first row completed.

    Stretch

    • Write Smaller in the same way as Larger and use both in one expression.

    English as an additional language

    • Check return and returns; show a vending machine returning change as a value handed back.

    In the lesson window

    • Think it through: Suppose a procedure works out 3 × 6 = 18 and OUTPUTs it. Could the program then add this 18 to the cost of 2 tickets? Why, or why not? What would the subroutine have to do instead?
    • Check your understanding: Which line correctly uses the TicketCost function?
  6. 5. Who can see which variable? Predict 11 min

    Purpose: Pupils predict a program that uses a local and a global variable, trace it, see why a local variable cannot be used outside, and decide what a same-named local variable does.

    Ask

    • What does the AddBonus program output? Would OUTPUT Bonus work at the end?
    • What does the Reset program output when both variables are called Level?

    Responses to expect

    • 14
    • No: Bonus exists only while the procedure runs
    • 5, because the local Level is separate

    Misconceptions to challenge

    • A learner may believe: “A variable declared in a procedure can be used in the main program.” Respond: A local variable is created when the procedure runs and is gone when it ends.
    • A learner may believe: “Two variables with the same name cause an error.” Respond: They live in different places, so the computer can tell them apart.
    • A learner may believe: “Reset changes the main program's Level.” Respond: The Level declared inside Reset is a different variable, so the global one still holds 5.
    • A learner may believe: “Nothing is output because Reset runs.” Respond: Reset is called, but OUTPUT is in the main program and shows the global Level.

    On the board or with the pen

    • Draw two boxes labelled Main program and AddBonus; put Score in the outer region and Bonus inside AddBonus only; erase Bonus when the call ends. Draw Level twice for the Reset program.

    Checking understanding: The same-name question: each wrong option is a different belief (error, global changed, no output); the correct answer rests on the idea that the two Levels are separate.

    Depending on the answers

    • If pupils choose 0: ask which Level the line Level ← 0 refers to: the one declared at the top or the one declared inside Reset?
    • If pupils are secure: ask what the program outputs if the DECLARE inside Reset is removed.

    Support

    • Give pupils a picture of the two scopes to label with Score and Bonus.

    Stretch

    • Why is it better for a procedure to use parameters than the global variable directly?

    English as an additional language

    • Check local and global; compare with a local shop and a global company.

    In the lesson window

    • Think it through: Predict what the program outputs. Then say whether a final line OUTPUT Bonus, added at the end of the program, would work, and why.
    • Apply it: What does this program output?
  7. 6. Subroutines that come ready-made Discover 12 min

    Purpose: Pupils predict rounding before ROUND is named, then use DIV, MOD, ROUND and RANDOM and work out the range of a dice formula.

    Ask

    • Round 7.456 to 1 decimal place and to 0 decimal places.
    • What do DIV(130, 60) and MOD(130, 60) give?
    • What are the smallest and largest values of ROUND(RANDOM() * 5, 0) + 1?

    Responses to expect

    • 7.5 and 7
    • 2 and 10, so 2 h 10 min
    • 1 and 6

    Misconceptions to challenge

    • A learner may believe: “The dice range is 0 to 5 because RANDOM() * 5 goes from 0 to 5.” Respond: That is before + 1; adding 1 moves both ends.
    • A learner may believe: “The range is 1 to 5 because RANDOM() never reaches 1.” Respond: It can be as large as 1, so ROUND(5, 0) + 1 gives 6.
    • A learner may believe: “ROUND(9.7, 0) is 9 because decimals are dropped.” Respond: Rounding goes to the nearest: 9.7 is closer to 10.
    • A learner may believe: “Library routines must be written by you.” Respond: They are already written; you only use them.

    On the board or with the pen

    • Write the minimum and the maximum of each part of the dice formula in a column: RANDOM() 0 to 1, times 5 gives 0 to 5, ROUND gives 0 to 5, plus 1 gives 1 to 6.

    Checking understanding: The dice-range check: the first option stops before the + 1, the third stops before the top end; both are answered by working each part's range.

    Depending on the answers

    • If pupils choose 0 and 5: ask them to add 1 to both and read the formula again.
    • If pupils are secure: ask for a formula giving a random whole number from 1 to 10.

    Support

    • Provide the first two rows of the library table completed.

    Stretch

    • The Python note says 1 and 6 come up half as often as the others. Why would that be, and what does it mean for a fair dice?

    English as an additional language

    • Check random, round and routine; roll a real dice.

    In the lesson window

    • Think it through: A calculator shows 7.456. Predict the number you get when you round it to 1 decimal place, and to 0 decimal places (a whole number).
    • Check your understanding: What are the smallest and largest values that Die ← ROUND(RANDOM() * 5, 0) + 1 can hold?
  8. 7. Programs that are easy to change Explain 7 min

    Purpose: Pupils say why a program built from subroutines is easier to look after, then pick the change that most improves a program with unclear names.

    Ask

    • Give two reasons why a program built from subroutines is easier to look after.
    • A program uses X, Y and Z with no comments: which change makes it most maintainable?

    Responses to expect

    • One change in one place, each part testable, names explain purpose, reuse
    • Rename the variables and add comments

    Misconceptions to challenge

    • A learner may believe: “A shorter program is easier to maintain.” Respond: Blank lines and indentation help a reader; shortness is not the aim.
    • A learner may believe: “Putting everything in one subroutine called Main is neat.” Respond: It is the same long list under a new name; the aim is smaller parts with clear names.

    On the board or with the pen

    • Write X, Y, Z in a column and ask pupils to supply names; write the comment above TicketCost.

    Checking understanding: The maintainable-change question: the wrong options are removing blank lines and one giant Main; the correct one offers names and comments.

    Depending on the answers

    • If pupils choose the shorter program: ask which version they could understand in six months and why.

    English as an additional language

    • Check maintain and identifier; use a school example of someone who has to change your work.

    In the lesson window

    • Think it through: Think back to the school notices in step 1 and the TicketCost function. Give two reasons why a program built from subroutines is easier to look after than one long list of lines.
    • Check your understanding: A program uses the identifiers X, Y and Z, with no comments. Which change makes it most maintainable?
  9. 8. Spot the mistake Apply 6 min

    Purpose: Pupils find the reason Sam's Double function does not work: it outputs instead of returning.

    Ask

    • What is wrong with Sam's function and how is it fixed?
    • What would Double(7) + 1 be if the function returned correctly?

    Responses to expect

    • OUTPUT shows the value but does not return it; use RETURN
    • 15

    Misconceptions to challenge

    • A learner may believe: “The call should use CALL.” Respond: A function call is part of an expression; CALL is for procedures.
    • A learner may believe: “Number should be global.” Respond: The parameter is fine: it receives 7.
    • A learner may believe: “OUTPUT and RETURN are the same.” Respond: OUTPUT shows a value on the screen; RETURN gives it to the line that called the function.

    On the board or with the pen

    • Write Double(7) + 1 and replace the call by a blank box; ask what goes in the box if the function only outputs.

    Checking understanding: The no-return question has each wrong option rooted in a different mix-up (CALL, global, return).

    Depending on the answers

    • If pupils choose CALL: ask where the value 14 must go in the expression.
    • If pupils fix quickly: ask them to write a correct version and trace Double(7) + 1.

    Support

    • Show the corrected function beside the faulty one so that pupils compare line by line.

    In the lesson window

    • Apply it: What is wrong with Sam’s function, and how is it fixed?
  10. 9. Write one yourself Apply 10 min

    Purpose: Pupils write a function that returns a ticket price from an age and use it in a loop to total three visitors.

    Ask

    • Write TicketPrice(Age) and a program that totals three visitors.
    • What does it output for ages 9, 70 and 30?

    Responses to expect

    • A function with a local Price, an IF chain, and RETURN Price
    • A FOR loop of 3 with Total ← Total + TicketPrice(Age)
    • Total: 19

    Misconceptions to challenge

    • A learner may believe: “The function should OUTPUT the price.” Respond: Then the total could not be calculated. RETURN gives the value back to the expression.
    • A learner may believe: “The age 65 gets the 8 price.” Respond: 65 or over pays 6: test with >= 65.
    • A learner may believe: “Total goes inside the loop.” Respond: Set it to 0 before the loop, as in Lesson 6.

    On the board or with the pen

    • Write the function as pupils dictate it, then trace the three ages with a column for Age, TicketPrice and Total.

    Checking understanding: Collect the function: look for RETURNS INTEGER, a local variable, edge ages at 12 and 65, and the call inside an expression.

    Depending on the answers

    • If pupils test only 9, 70 and 30: ask which ages 11, 12, 64 and 65 give and whether the function is right at the edges.
    • If pupils finish quickly: ask them to write the program so that the number of visitors is typed in first.

    Support

    • Provide the first three lines of the function and ask pupils to complete it.

    Stretch

    • Rewrite TicketPrice without a local variable by returning inside each branch. Why does the lesson prefer one RETURN at the end?

    In the lesson window

    • Think it through: Write a function TicketPrice that takes an age and returns the price. Then write the rest of a program that reads the ages of 3 visitors and outputs the total. Say what it outputs for ages 9, 70 and 30.
  11. 10. Show what you know Consolidate 7 min

    Purpose: Pupils explain procedure against function and parameter against argument, then choose the right kind of subroutine for a new library scenario before the summary.

    Ask

    • Explain the difference between a procedure and a function, and between a parameter and an argument, with the cinema or the concert.
    • A heading shown in many places and a fine for a late book: which pair of subroutines?

    Responses to expect

    • A procedure does a task and a function returns a value
    • The parameter is in the definition, the argument is in the call
    • A procedure for the heading and a function for the fine

    Misconceptions to challenge

    • A learner may believe: “Two procedures are enough because both show something.” Respond: The fine is needed as a number to add up or compare; a procedure that only displays it cannot hand it back.
    • A learner may believe: “A function for the heading and a procedure for the fine.” Respond: It is the other way round: the heading is a task; the fine is a value.

    Checking understanding: Collect the explanations: check that the parameter and argument are told apart by where they appear.

    Depending on the answers

    • If pupils choose a function for the heading: ask whether the program needs anything back from showing a heading.
    • If several are unsure of CALL against a function call: begin the next lesson with three one-line examples to sort into correct and incorrect.

    In the lesson window

    • Think it through: Explain to a friend the difference between a procedure and a function, and between a parameter and an argument. Use the cinema or the concert as your example.
    • Apply it: A fictional library program needs to show a heading in the same style in many places, and to work out the fine for a book that is a given number of days late. Which pair of subroutines is the best choice?
  12. Lesson recap Closing

    On screen: the lesson’s objectives again; Next adds the closing question, which asks the class to explain each one in their own words without looking back. It says nothing about what anyone has learnt: use the answers to decide what to revisit.

    Points to listen for

    • A procedure carries out a task and is called with CALL; a function returns one value with RETURN and is called inside an expression.
    • A parameter is in the definition; an argument is in the call; they are matched by position and type.
    • A local variable exists only inside its subroutine; a global variable can be used everywhere; a local variable with the same name is separate.
    • DIV, MOD, ROUND and RANDOM are ready-made library routines.
    • Subroutines, meaningful names and comments make a program easy to change.

Consolidation

  • A procedure carries out a task and is called with CALL; a function returns one value with RETURN and is called inside an expression.
  • A parameter is in the definition; an argument is in the call; they are matched by position and type.
  • A local variable exists only inside its subroutine; a global variable can be used everywhere; a local variable with the same name is separate.
  • DIV, MOD, ROUND and RANDOM are ready-made library routines.
  • Subroutines, meaningful names and comments make a program easy to change.