Level:
Correct: 0
Incorrect: 0
Marks: 0/0
Exam Practice
Subject:
Topics:
Avg
score:
–
Topics
done:
0/32

Procedures, functions and parameters

Practise 38 exam-style questions on Procedures, functions and parameters for Cambridge IGCSE Computer Science (Paper 2). Write your answer as you would in the exam and get it marked in seconds against the mark scheme, point by point, with an AI tutor on hand if you get stuck.

  • 38 questions
  • 1–8 marks each
  • Instant marking
  • No sign-up needed
20 questions, marked as you go

Sample questions with model answers

Here are 4 of the questions in this topic, each with a model answer, an explanation of why it earns the marks and the mark scheme an examiner would use.

Question 1

2 marks

Describe the difference between a procedure and a function.

Model answer

A function always returns a single value to the place it was called from, so it is used inside an expression, for example Area ← CalculateArea(4.0, 2.5). A procedure carries out a task but does not return a value, and it is run with a CALL statement, for example CALL ShowMenu.

Explanation

Both are named blocks of code that can be called many times. The key difference is the returned value: a function header ends with RETURNS and a data type, and its body uses RETURN. A common mistake is to say 'a function has parameters and a procedure does not' - both can have parameters, or none.

Mark scheme
  1. 1 mark

    A function returns a (single) value to the statement that called it

    max 2

  2. 1 mark

    A procedure does not return a value / a procedure just carries out a task

  3. 1 mark

    A function is called as part of an expression or assignment (e.g. Result ← Square(5)) whereas a procedure is called using CALL

Question 2

2 marks

A student writes a procedure, ShowDiscountPrice, that uses fixed values for the price and the discount inside the procedure. A second student writes it with two parameters, Price and Discount.

Explain why the version with parameters is more useful.

Model answer

With parameters, the same procedure can be called many times with a different price and discount each time, e.g. CALL ShowDiscountPrice(20.00, 10). The values are passed in when it is called, so the procedure does not need to be rewritten and does not depend on global variables.

Explanation

Parameters make a subroutine general: one definition handles any data. Without them, the procedure either works for only one case or has to read global variables, which makes the program harder to follow and maintain.

Mark scheme
  1. 1 mark

    The same procedure can be reused / called many times with different values (prices and discounts)

    max 2

  2. 1 mark

    Values are passed in when it is called, so it does not need to rely on global variables / it is self-contained

  3. 1 mark

    The values do not have to be changed inside the procedure / the code does not need rewriting for each new price

Question 3

3 marks

This function should return "Child" for ages under 13, "Teen" for ages 13 to 19 and "Adult" for ages over 19.

FUNCTION Category(Age : INTEGER) RETURNS STRING
    IF Age < 13
      THEN
        RETURN "Child"
    ENDIF
    IF Age > 19
      THEN
        RETURN "Adult"
    ENDIF
ENDFUNCTION

Explain the problem with this function and write a corrected version.

Model answer

For an age from 13 to 19 neither condition is true, so the function reaches ENDFUNCTION without returning anything.

FUNCTION Category(Age : INTEGER) RETURNS STRING
    DECLARE Group : STRING
    IF Age < 13
      THEN
        Group ← "Child"
      ELSE
        IF Age > 19
          THEN
            Group ← "Adult"
          ELSE
            Group ← "Teen"
        ENDIF
    ENDIF
    RETURN Group
ENDFUNCTION

Explanation

Every possible path through a function must end in a RETURN, or the calling statement receives no value. Storing the result in a local variable and returning it once at the end is the safest structure. Test functions with data from every category, including the boundaries 12, 13, 19 and 20.

Mark scheme
  1. 1 mark

    MP1 for ages 13 to 19 no RETURN statement is reached, so the function does not return a value

  2. 1 mark

    MP2 a corrected function returns "Teen" for ages 13 to 19

    e.g. RETURN "Teen" before ENDFUNCTION, or an ELSE branch

  3. 1 mark

    MP3 the complete corrected function still returns "Child" and "Adult" correctly, and returns a value for every age

Question 4

5 marks

Write a function, Grade, that takes a test mark (0 to 100) as an integer parameter and returns a single character:
'A' for a mark of 80 or more
'B' for a mark from 60 to 79
'C' for a mark from 40 to 59
'U' for any other mark

Model answer

FUNCTION Grade(Mark : INTEGER) RETURNS CHAR
    DECLARE Letter : CHAR
    IF Mark >= 80
      THEN
        Letter ← 'A'
      ELSE
        IF Mark >= 60
          THEN
            Letter ← 'B'
          ELSE
            IF Mark >= 40
              THEN
                Letter ← 'C'
              ELSE
                Letter ← 'U'
            ENDIF
        ENDIF
    ENDIF
    RETURN Letter
ENDFUNCTION

Explanation

Testing from the highest boundary down means each ELSE already knows the mark is below the previous boundary, so only one comparison is needed per grade. Storing the grade in a local variable and returning it once at the end guarantees a value is returned for every mark. Characters are written in single quotes.

Mark scheme
  1. 1 mark

    MP1 header with one INTEGER parameter and RETURNS CHAR

    Accept RETURNS STRING.

  2. 1 mark

    MP2 'A' for a mark of 80 or more

    Boundaries must be exact, e.g. Mark >= 80, not Mark > 80.

  3. 1 mark

    MP3 'B' for a mark from 60 to 79

  4. 1 mark

    MP4 'C' for a mark from 40 to 59 and 'U' for any lower mark

  5. 1 mark

    MP5 the grade is returned in every case, and ENDFUNCTION

    Do not accept OUTPUT instead of RETURN.

Ready to try the rest? Every answer you write is marked against the mark scheme, so you can see exactly where the marks are.

AI Tutor Hints first, answers after you try

Hi! Stuck? Ask me for a hint and I’ll help you work it out. Once you’ve checked your answer I can explain exactly where the marks are.