Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  Procedures, functions, parameters and scope
Lesson pack · IGCSE Computer Science 0478 · 8.1 · IGCSE

Procedures, functions, parameters and scope

Write it once. Large programs are built from small named blocks of code.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.1
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 6 of 11 in Programming; follows Arithmetic, relational and logical operators; leads into Library routines: MOD, DIV, ROUND and RANDOM
Central model
Fig. 1: control passes to the subroutine and back; a function also hands back a value
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • 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.
Key points to leave on the board
  • 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.
The core visual model

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.

main program CALL Stars(3)OUTPUT "Done"X ← Square(4)OUTPUT X PROCEDURE Stars runs, returns no value FUNCTION Square RETURNS INTEGER control returns 16 returned a procedure is CALLed; a function is used inside an expression
Fig. 1: control passes to the subroutine and back; a function also hands back a value
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen 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 minTeach and check: Procedures and CALLIn 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 minTeach and check: Functions return a valueA 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 minTeach and check: Local and global variablesThe 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 minCheckThe 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 minPlenaryThe 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 minExit and homeworkSet the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

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.

Running the cooperative task

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.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching 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.
Worksheet

Procedures, functions, parameters and scope: worksheet

6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · explain[3 marks]

Explain the difference between a procedure and a function, including how each is called.

Q2 · recall[2 marks]

Describe what is meant by a local variable and a global variable.

Q3 · write[4 marks]

Write a function Average that takes three integer parameters and returns their mean as a real number.

Q4 · trace[3 marks]

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))
Q5 · write[3 marks]

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.

Q6 · analysis[2 marks]

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
Q1 · 3 marks
  • 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.
Q2 · 2 marks
  • 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.
Q3 · 4 marks
  • FUNCTION Average(A : INTEGER, B : INTEGER, C : INTEGER)
  • RETURNS REAL at the end of the header
  • RETURN (A + B + C) / 3 (brackets needed)
  • ENDFUNCTION
Q4 · 3 marks
  • X ← Double(3) = 6.
  • Show(6 + 1) outputs Value: 7.
  • Double(6) = 12, so Show outputs Value: 12.
Q5 · 3 marks
  • PROCEDURE Greet(Name : STRING, Times : INTEGER)
  • FOR Count ← 1 TO Times, OUTPUT "Hello ", Name, NEXT Count, ENDPROCEDURE
  • CALL Greet("Sam", 2)
Q6 · 2 marks
  • CALL is only used for procedures; a function is used inside an expression.
  • Correct: Total ← Add(2, 3).
Quiz

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.

Q11 mark

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
Q21 mark

FUNCTION Square(N : INTEGER) RETURNS INTEGER returns N * N. Which statement uses it correctly?

  • ATotal ← Square(5)
  • BCALL Square(5)
  • CSquare(5) ← Total
  • DCALL Total ← Square(5)
Q31 mark

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
Q41 mark

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
Q51 mark

What is output?

FUNCTION Twice(N : INTEGER) RETURNS INTEGER
    RETURN N * 2
ENDFUNCTION

OUTPUT Twice(Twice(3) + 1)

  • A14
  • B7
  • C16
  • D13
Q61 mark

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
Q1: A

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.

Q2: A

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.

Q3: A

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.

Q4: A

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.

Q5: A

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.

Q6: A

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.

Lab and demo

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

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.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonArithmetic, relational and logical operators