CIE IGCSE | 8.1.6A Procedures, Functions and Parameters

Lesson objective

Explain procedures, functions and parameters, distinguish returning from outputting, and trace how supplied values and results move between routines and their caller.

Learn

8.1.6A | PROCEDURES, FUNCTIONS AND PARAMETERS

01 | WHY GIVE A TASK ITS OWN NAME?

Imagine a quiz that greets a player, checks answers and calculates a final score. If the same greeting is needed in several places, copying its instructions everywhere makes the program longer and harder to update.

Instead, give that task a name and define its instructions once. Other parts of the program can ask that named task to run whenever needed. A routine, also called a subroutine, is a named group of instructions that performs a particular task.

Procedures and functions are two kinds of routine. Both help divide a larger program into understandable parts. Their key difference is whether they return a value for the calling expression to use.

02 | A PROCEDURE PERFORMS A TASK

A procedure is a named routine that performs a task without returning a value to replace its call in an expression. Its task may display a message, draw something or carry out a sequence of actions.

The example defines a procedure called ShowWelcome. Its body is the instruction or group of instructions between PROCEDURE and ENDPROCEDURE. Here the body contains one OUTPUT instruction.

A definition states what the procedure will do. It does not, by itself, output the welcome message. Another instruction must call the procedure.

PROCEDURE ShowWelcome
  OUTPUT "Welcome to the quiz"
ENDPROCEDURE

03 | A CALL ASKS THE PROCEDURE TO RUN

A call is an instruction or expression that starts a routine. The caller is the part of the program making that call. In Cambridge pseudocode, a procedure is called using CALL.

Execution reaches CALL ShowWelcome, runs the procedure’s body and then continues at the instruction after that call. The outputs are “Before greeting”, “Welcome to the quiz” and “After greeting”, in that order.

The definitions are placed at the start of the code. The main program contains the instructions that coordinate the overall task, including calls to those routines.

PROCEDURE ShowWelcome
  OUTPUT "Welcome to the quiz"
ENDPROCEDURE

OUTPUT "Before greeting"
CALL ShowWelcome
OUTPUT "After greeting"

04 | PARAMETERS MAKE THE SAME TASK FLEXIBLE

A fixed welcome message is useful, but a personalised greeting is better. A parameter is a named item in a routine’s definition that receives a value supplied when the routine is called.

PlayerName below is a STRING parameter. The procedure uses it in its body, so the same instructions can greet different people.

At the first call, PlayerName receives “Maya”. At the second, it receives “Leo”. The outputs are “Hello, Maya” and “Hello, Leo”. We have reused one task with different supplied values.

PROCEDURE Greet(PlayerName : STRING)
  OUTPUT "Hello, ", PlayerName
ENDPROCEDURE

CALL Greet("Maya")
CALL Greet("Leo")

05 | PARAMETER NAME AND SUPPLIED VALUE

The parameter is the name written in the definition. The actual value or expression supplied in the call is often called an argument. Cambridge examples also describe these as parameter values. Understanding their roles matters more than treating the terms as rival labels.

In CALL Greet("Maya"), PlayerName is the parameter and “Maya” supplies its value. A caller can supply a variable too: CALL Greet(StudentName) uses the current value of StudentName. The names do not need to match.

Do not confuse a parameter with user input. A parameter receives information from the caller. INPUT receives information from outside the program, such as the user. A routine can use either mechanism, but they are different operations.

The definition provides a name; the call provides its value
LocationItemRole
DefinitionPlayerName : STRINGNames the parameter and its expected type
Call"Maya"Supplies a STRING value
CallStudentNameSupplies that variable’s current value

06 | MATCH NUMBER, ORDER AND TYPE

A procedure can have several parameters. Their order in the definition determines which supplied value belongs to each name. Their declared types tell the caller what kinds of values to supply.

For this example, the first supplied value is a STRING name and the second is an INTEGER number of points. CALL ShowScore("Maya", 12) displays “Maya scored 12”. Reversing those arguments would put the wrong types in the positions.

The syllabus allows procedures and functions with or without parameters, with up to three parameters. Use only the information a routine needs.

PROCEDURE ShowScore(PlayerName : STRING, Points : INTEGER)
  OUTPUT PlayerName, " scored ", Points
ENDPROCEDURE

CALL ShowScore("Maya", 12)

07 | A FUNCTION RETURNS A VALUE

A function is a named routine that returns a single value to the point where it is called. Its call can therefore be used as part of an expression, such as a calculation, assignment or output expression.

The function Double takes an INTEGER parameter and returns an INTEGER result. RETURNS INTEGER in its header states the result’s type. RETURN in the body supplies the actual result.

When Double(6) is evaluated, Number receives six, the body calculates twelve and RETURN sends twelve back. The calling expression then uses twelve in place of Double(6).

FUNCTION Double(Number : INTEGER) RETURNS INTEGER
  RETURN Number * 2
ENDFUNCTION

DECLARE Result : INTEGER
Result ← Double(6)
OUTPUT Result

08 | RETURN IS NOT OUTPUT

OUTPUT communicates information to the user. RETURN supplies a function’s result to its caller. A calculation can return a result without displaying anything.

In Result ← Double(6), twelve is stored in Result. No screen output is caused by that assignment. A later OUTPUT Result displays it.

A procedure that displays twelve has performed an output task, but it has not supplied twelve as a function result for another calculation. This distinction is important when deciding whether a routine should be a procedure or function.

Returning, storing and displaying are separate actions
InstructionEffect
RETURN Number * 2Returns the calculated result to the calling expression
Result ← Double(6)Stores the returned value in Result
OUTPUT ResultDisplays the value currently held in Result

09 | USE A FUNCTION INSIDE AN EXPRESSION

A returned value can be used immediately rather than stored first. In Total ← Double(6) + 3, the function call supplies twelve, then the caller adds three and stores fifteen.

In Cambridge pseudocode, do not use CALL for a function. A function call belongs in an expression; CALL is used for a procedure as a complete statement.

Function results are not limited to numbers. A function can return BOOLEAN for a check or STRING for generated text, provided its returned value matches its declared result type.

Total ← Double(6) + 3
OUTPUT Double(4)

// First assignment stores 15
// OUTPUT displays 8

10 | COMPARE THE TWO KINDS OF ROUTINE

Ask what the caller needs. If it needs an action performed, such as displaying a greeting, use a procedure. If it needs a value to use, such as a calculated price, use a function.

Both can receive parameters, and both have definitions and bodies. The difference is not that procedures are always simple or functions always mathematical. It is the returned value and how the call is used.

Choose by what the caller needs from the routine
FeatureProcedureFunction
PurposePerform a taskProduce a value for the caller
Cambridge callCALL Greet("Maya")Cost ← TicketCost(3)
Value returned to calling expressionNo function resultOne value
ParametersMay have none or severalMay have none or several
Result type in headerNo RETURNS result typeRETURNS states the result type

11 | A TICKET CALCULATION WITH TWO PARAMETERS

To calculate ticket cost, the function needs the number of tickets and the price of each. Quantity is INTEGER, Price is REAL and the returned cost is REAL.

TicketCost(3, 4.50) assigns three to Quantity and 4.50 to Price for that call. The body multiplies them and returns 13.5. This is a numeric value; displaying it as 13.50 would be a separate formatting choice.

The function has no INPUT or OUTPUT instruction. Its caller supplies the information and decides what to do with the result.

FUNCTION TicketCost(Quantity : INTEGER, Price : REAL) RETURNS REAL
  RETURN Quantity * Price
ENDFUNCTION

DECLARE Cost : REAL
Cost ← TicketCost(3, 4.50)
OUTPUT "Cost: ", Cost

12 | STEP THROUGH A FUNCTION CALL

Choose the quantity and price, then follow the call. The demo separates the supplied arguments, the parameter values, the returned result and the caller’s final assignment.

These controls accept a non-negative whole-number quantity and a non-negative price. They validate the demo’s input; the simple pseudocode function above assumes suitable values have already been supplied. Price uses two decimal places here.

    13 | THE MAIN PROGRAM CONNECTS THE PARTS

    Procedures and functions work together. This complete example asks the user for a name and quantity, greets them with a procedure, calculates the cost with a function and outputs the result.

    Greet receives a value but does not return one. TicketCost receives two values and returns a calculated cost. The main program controls the input and output sequence. Assume the quantity supplied is a valid non-negative INTEGER.

    PROCEDURE Greet(PlayerName : STRING)
      OUTPUT "Hello, ", PlayerName
    ENDPROCEDURE
    
    FUNCTION TicketCost(Quantity : INTEGER, Price : REAL) RETURNS REAL
      RETURN Quantity * Price
    ENDFUNCTION
    
    DECLARE Name : STRING
    DECLARE Tickets : INTEGER
    DECLARE Cost : REAL
    OUTPUT "Enter your name"
    INPUT Name
    OUTPUT "Enter ticket quantity"
    INPUT Tickets
    CALL Greet(Name)
    Cost ← TicketCost(Tickets, 4.50)
    OUTPUT "Cost: ", Cost

    14 | WHY THIS ORGANISATION HELPS

    Reuse: TicketCost can calculate the cost at several points without copying its calculation. Different arguments allow the same routine to handle different quantities and prices.

    Readability: a meaningful name communicates a task’s purpose. A reader can understand the main program’s sequence before inspecting every instruction inside its routines.

    Maintenance and testing: a calculation can be updated in its definition and checked with suitable examples. This reduces duplicated logic, but it does not automatically prove the program correct. Changed behaviour must still suit every caller.

    Passing explicit parameters makes the routine’s required information visible. Variable scope and how changes to variables behave are explored in the later local-and-global-variables lesson; do not assume a routine automatically changes the caller’s variables.

    15 | AVOID THE COMMON MIX-UPS

    A definition describes a routine; a call runs it. A parameter names incoming information; an argument supplies its value. RETURN sends a function result to the caller; OUTPUT displays information.

    Do not write CALL TicketCost(3, 4.50) when using this function. Write a call in an expression, such as Cost ← TicketCost(3, 4.50). Do not try to assign a value from a procedure that returns no function result.

    Match argument count, order and types to the definition. Do not confuse RETURNS, which states the type in a function header, with RETURN, which supplies the result from its body.

    You should now be able to explain the three core ideas. The next two lessons develop defining and calling procedures, then defining and calling functions, in more detail.

    MATCH THE ROUTINE TERM TO ITS ROLE

    Terminology

    Terminology

    Routine

    A named group of instructions performing a task; also called a subroutine.

    Procedure

    A named routine performing a task without returning a value to replace its call in an expression.

    Function

    A named routine returning a single value to its calling expression.

    Definition

    The code stating a routine’s name, interface and body.

    Call

    An instruction or expression that starts a routine.

    Caller

    The part of a program making a routine call.

    Body

    The instructions within a routine that execute when it is called.

    Parameter

    A named item in the definition receiving a supplied value at a call.

    Argument

    A value or expression supplied at a call; also described as a parameter value.

    Parameter type

    The kind of value expected for a parameter.

    Return type

    The data type of a function’s returned result.

    RETURN

    Supplies a function’s result to the point of its call.

    RETURNS

    States the returned value’s data type in a function header.

    OUTPUT

    Communicates information to the user rather than returning a function result.

    Main program

    The instructions coordinating the overall task, including calls to routines.

    Expression

    Values and operations evaluated to produce a result.

    Reuse

    Using the same defined routine at different calls instead of duplicating its logic.

    Modular organisation

    Dividing a program into parts with clear tasks and connections.

    Questions

    Questions

    CHECK YOUR UNDERSTANDING

    Select all correct choices. Each exact set earns one point.

    1. Which statements about routines are correct?
    2. Which distinguish these procedures and functions?
    3. Greet has parameter PlayerName : STRING. Which are correct?
    4. ShowScore expects STRING then INTEGER. Which calls match?
    5. Double returns its INTEGER parameter multiplied by two. Which are valid function uses?
    6. What happens in Cost ← TicketCost(3, 4.50)?
    7. Which statements about RETURN and RETURNS are correct?
    8. Which are benefits of named routines?

    EXPLAIN AND COMPARE

    Write your answers before revealing each sample.

    1. Define a procedure and a function, identifying their main difference.

    2. Explain the difference between defining and calling a procedure.

    3. Identify the parameter and supplied argument in Greet(PlayerName : STRING) and CALL Greet("Maya").

    4. Explain why RETURN and OUTPUT are different.

    5. Calculate Total after Total ← Double(6) + 3, where Double returns twice its parameter.

    6. Describe how arguments are matched to multiple parameters.

    7. Suggest whether a greeting and a calculated ticket cost should be a procedure or function.

    8. Explain a maintenance benefit of using one named calculation routine.

    BUILD IT | SCORE WITH A BONUS

    Design a function AddBonus with INTEGER parameters Score and Bonus that returns their sum as INTEGER. Use it to store a result for score twelve and bonus five, then pass that result to a procedure ShowResult with an INTEGER parameter that displays it. Write definitions before the main instructions. Predict which routine returns a value and which routine outputs it.

    Reveal one possible solution
    FUNCTION AddBonus(Score : INTEGER, Bonus : INTEGER) RETURNS INTEGER
      RETURN Score + Bonus
    ENDFUNCTION
    
    PROCEDURE ShowResult(Points : INTEGER)
      OUTPUT "Final score: ", Points
    ENDPROCEDURE
    
    DECLARE FinalScore : INTEGER
    FinalScore ← AddBonus(12, 5)
    CALL ShowResult(FinalScore)

    AddBonus receives twelve and five, then returns seventeen. The caller assigns seventeen to FinalScore. ShowResult receives that value as Points and outputs “Final score: 17”. The function does not display the result, and the procedure does not supply a returned value for assignment.

    Flashcards

    Flashcards

    Click to flip. Select the ideas you need to revisit.

    0 cards selected for revision.

      Selections are kept while this page is open.

      Workbook

      Workbook

      COMING SOON

      The workbook for 8.1.6A Procedures, Functions and Parameters is coming soon. Complete the matching task and written questions in your exercise book.