Year 10 · Computer Science · Summer 1
Programming · Lesson 4 of 5
Procedures, functions and scope
Practical8.1 Programming concepts
Year 10 · Computer Science · Programming · Lesson 4 of 5
You will learn to
- Explain the difference between a procedure and a function.
- Write a procedure with a parameter, and a function that returns a value.
- Explain local and global scope, and find a bug caused by scope.
- Write maintainable code, with meaningful names, comments and indentation.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Success criteria
You have got it when you can say:
- I can write a procedure that prints a menu, and call it.
- I can write a function that returns the average of three numbers.
- I can explain why a local variable cannot be used outside its subroutine.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
FOR i ← 1 TO 10 … NEXT i runs the loop…
A 9 timesB 11 timesC 10 times
C. 10 times
- Last unit
The four stages of the program development life cycle are…
A Fetch, decode, execute, storeB Input, process, output, storageC Analysis, design, coding, testing
C. Analysis, design, coding, testing
- Earlier this year
How many bits are in a byte?
A 4B 16C 8
C. 8
Year 10 · Computer Science · Programming · Lesson 4 of 5
Key words (1 of 5)
- Variable
- A named place in memory whose value can change while the program runs.
- Constant
- A named value that is set once and cannot change while the program runs.
- Identifier
- The name given to a variable, constant, array or subroutine.
- Data type
- The kind of value stored: INTEGER, REAL, CHAR, STRING or BOOLEAN.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Key words (2 of 5)
- Assignment
- Storing a value in a variable, written with ← in pseudocode.
- Sequence
- Statements run one after another in the order they are written.
- Selection
- Choosing which statements to run using a condition (IF or CASE).
- Iteration
- Repeating a block of statements in a loop.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Key words (3 of 5)
- Count-controlled loop
- A FOR loop that repeats a set number of times.
- Pre-condition loop
- A WHILE loop that checks its condition before each pass, so it may run zero times.
- Post-condition loop
- A REPEAT UNTIL loop that checks its condition after each pass, so it runs at least once.
- Totalling
- Keeping a running total by adding each value to a total variable.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Key words (4 of 5)
- Counting
- Adding 1 to a counter variable each time something happens.
- Procedure
- A named subroutine that does a task and is run with CALL; it does not return a value.
- Function
- A named subroutine that returns a single value to where it was called.
- Parameter
- A variable in a subroutine heading that receives a value when the subroutine is called.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Key words (5 of 5)
- Local variable
- A variable declared inside a subroutine that can only be used inside it.
- Global variable
- A variable declared in the main program that can be used anywhere in the program.
- Library routine
- A ready-made, tested subroutine provided with the language, such as ROUND or RANDOM.
- Array
- A data structure holding many values of the same data type under one name, each found by its index.
The explanation · step 1 of 23 · part 1 of 3
Variables: named boxes in memory
A variable is a named place in memory that holds one value. The value can change while the program runs.
The name is called an identifier. Cambridge pseudocode writes identifiers in PascalCase, such as `TotalScore`.
`DECLARE` gives the name and the data type before the variable is used.
The arrow `←` means assignment: work out the value on the right, then store it in the variable on the left.
The explanation · step 1 of 23 · part 2 of 3
Variables: named boxes in memory (continued)
DECLARE Score : INTEGER Score ← 0 Score ← Score + 5 OUTPUT Score
score = 0 score = score + 5 print(score)
The explanation · step 1 of 23 · part 3 of 3
Variables: named boxes in memory (continued)
Python does not need a declaration. It works out the type from the value. Cambridge pseudocode declares every variable so the type is clear to the reader.
Read `Score ← Score + 5` from right to left. It is not a maths equation. It means: work out Score + 5, then put the answer back in Score.
The explanation · step 2 of 23 · part 1 of 3
Constants: values that never change
A constant is a named value that is set once. It cannot change while the program runs.
In pseudocode write `CONSTANT`, the name, `←` and the value. No data type is given: the value shows it.
Python has no true constants. Programmers write the name in CAPITALS to mean: do not change this.
The explanation · step 2 of 23 · part 2 of 3
Constants: values that never change (continued)
CONSTANT VATRate ← 0.21 DECLARE Price : REAL Price ← 50.0 OUTPUT "Price with VAT: ", Price * (1 + VATRate)
VAT_RATE = 0.21
price = 50.0
print("Price with VAT:", price * (1 + VAT_RATE))The explanation · step 2 of 23 · part 3 of 3
Constants: values that never change (continued)
If the VAT rate changes, you edit one line instead of every line that uses 0.21. The name also tells the reader what 0.21 means. The program cannot change it by mistake.
Use a constant only for a value that is fixed for the whole run, like a tax rate or the size of an array. A value that the program updates, like a score, must be a variable.
The explanation · step 3 of 23 · part 1 of 4
The five data types
A data type says what kind of value a variable holds. It decides how the value is stored and what you can do with it.
Cambridge uses five data types. A CHAR goes in single quotes. A STRING goes in double quotes.
The explanation · step 3 of 23 · part 2 of 4
The five data types (continued)
| Data type | What it holds | Examples | Python |
|---|---|---|---|
| INTEGER | A whole number: positive, negative or zero | 25, -3, 0 | int |
| REAL | A number with a decimal part | 3.142, -0.5, 19.99 | float |
| CHAR | Exactly one character | 'A', '7', '?' | str of length 1 (no separate char type) |
| STRING | Zero or more characters (text) | "Madrid", "28013", "" | str |
| BOOLEAN | Only TRUE or FALSE | TRUE, FALSE | True, False |
The explanation · step 3 of 23 · part 3 of 4
The five data types (continued)
DECLARE Age : INTEGER DECLARE Height : REAL DECLARE Grade : CHAR DECLARE FirstName : STRING DECLARE IsMember : BOOLEAN Age ← 15 Height ← 1.68 Grade ← 'A' FirstName ← "Lucia" IsMember ← TRUE
The explanation · step 3 of 23 · part 4 of 4
The five data types (continued)
age = 15 height = 1.68 grade = "A" first_name = "Lucia" is_member = True print(type(age), type(height), type(grade), type(first_name), type(is_member))
The type decides which operations make sense. You can add two INTEGERs. You cannot multiply two names.
The explanation · step 15 of 23 · part 1 of 2
Choosing the right loop
First ask: do I know how many times? If yes, use FOR.
If not, ask: must it run at least once? If yes, use REPEAT UNTIL. If it might need to run zero times, use WHILE.
| Loop | Condition checked | Runs at least once? | Use it when | Example |
|---|---|---|---|---|
| FOR (count-controlled) | No condition: it counts | Runs a fixed number of times | You know how many times | Enter 30 marks |
| WHILE (pre-condition) | Before each pass | No, it can run 0 times | Unknown number of times, maybe none | Add sales until the target is reached |
| REPEAT UNTIL (post-condition) | After each pass | Yes, always | Unknown number, but at least one | Ask for a PIN until it is correct |
The explanation · step 15 of 23 · part 2 of 2
Choosing the right loop (continued)
If a question says 'use a pre-condition loop', you must use WHILE. Using a different loop loses marks, even if the program works.
The explanation · 19 more steps
More on the lesson page
The lesson page has the rest of the explanation, step by step:
- Choosing the right data type
- Input and output
- Sequence: one step after another
- Selection with IF
- Nested IF: up to three levels
- Selection with CASE
- Relational and logical operators
- Arithmetic operators, DIV and MOD
- Iteration: the count-controlled FOR loop
- The pre-condition WHILE loop
- The post-condition REPEAT UNTIL loop
- Totalling and counting
- String handling: LENGTH, UCASE, LCASE
- SUBSTRING and the first position: 1 or 0
- Procedures and parameters
- Functions: subroutines that return a value
- Local and global variables
- Library routines: MOD, DIV, ROUND, RANDOM
- Writing maintainable programs
Year 10 · Computer Science · Programming · Lesson 4 of 5
Quick quiz · 1 of 3
A REPEAT … UNTIL loop…
- A Runs for ever
- B May run zero times
- C Always runs at least once
C. Always runs at least once
The test is at the end, so the body runs first. WHILE tests first and may run zero times.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Quick quiz · 2 of 3
17 MOD 5 is…
- A 3.4
- B 2
- C 3
B. 2
MOD is the remainder: 17 = 3 × 5 + 2. DIV would give 3.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Quick quiz · 3 of 3
A function differs from a procedure because it…
- A Returns a value
- B Has a name
- C Can have parameters
A. Returns a value
Both have names and parameters. Only a function gives a value back.
Year 10 · Computer Science · Programming · Lesson 4 of 5
Exit ticket
Answer on paper before you go. Then check.
A local variable…
A Exists only inside its subroutineB Can be used anywhereC Never changes
A. Exists only inside its subroutine It is created when the subroutine starts and gone when it ends.
DECLARE Marks : ARRAY[1:30] OF INTEGER makes…
A 30 stringsB 30 whole-number slots, numbered 1 to 30C One number
B. 30 whole-number slots, numbered 1 to 30 Cambridge arrays start at 1 unless the question says otherwise.