HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Procedures, Functions and Strings in Pseudocode
  1. 1Procedures, functions and parameters
  2. 2Library routines and string handling
  3. 3Maintainable programs and the long scenario question
  1. Computer Science
  2. /Procedures, Functions and Strings in Pseudocode
  3. /Procedures, functions and parameters

Course 8.2·Lesson 1 of 3

Procedures, functions and parameters

A long program is easier to write and to test when it is cut into named parts. This lesson defines and calls procedures and functions in Cambridge pseudocode, passes values to them as parameters, and explains local and global variables.

about 25 min1 simulation1 practice question
Lesson 1 of 3Show all lessonsHide
Procedures, Functions and Strings in Pseudocode
  1. 1Procedures, functions and parameters
  2. 2Library routines and string handling
  3. 3Maintainable programs and the long scenario question

Try the code and the simulations beside the lesson.

Naming a job

A recipe book says "make the sauce (page 12)" instead of writing the sauce out in every recipe. A program does the same. A named block of code that does one job is a subroutine. You write it once and call it by name as often as you need. The Python Foundations chapter "Functions: Name a Recipe" teaches this in Python with def. Cambridge pseudocode has two kinds of subroutine.

  • A procedure does a job. It gives no value back.
  • A function does a job and returns one value to the place where it was called.

The syllabus puts procedures and functions at the start of the code, before the main program.

Procedures

A procedure starts with PROCEDURE and its name, and ends with ENDPROCEDURE. It may have parameters: names, each with a data type, for the values it is given. You run it with CALL.

pseudocode
PROCEDURE DefaultLine
    CALL Line(60)
ENDPROCEDURE

PROCEDURE Line(Size : INTEGER)
    DECLARE Length : INTEGER
    FOR Length ← 1 TO Size
        OUTPUT '-'
    NEXT Length
ENDPROCEDURE

CALL Line(25) passes the value 25 to the parameter Size, and 25 dashes are output. CALL DefaultLine has no parameters: it calls Line with 60. When the procedure ends, control goes back to the line after the call. A CALL is a complete statement on its own line.

The value in the call is the argument. Arguments are matched to the parameters in order, and each must have the right data type. In this syllabus a procedure or a function has at most three parameters.

Functions

A function says which type of value it returns, with RETURNS. The keyword RETURN gives the value back. It is usually the last statement.

pseudocode
FUNCTION SumSquare(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
    RETURN Number1 * Number1 + Number2 * Number2
ENDFUNCTION

OUTPUT "Sum of squares = ", SumSquare(10, 20)

SumSquare(10, 20) returns 10×10+20×20=50010 \times 10 + 20 \times 20 = 50010×10+20×20=500, so the output is Sum of squares = 500. A function call is not a statement on its own, and it never uses CALL. It is used inside an expression: in an OUTPUT, an assignment or a condition. The returned value takes the place of the call.

Two flowcharts. Left, the main program: Start, a rectangle Total ← 0, two subroutine boxes with double sides, AddBonus(45) and AddBonus(30), a parallelogram OUTPUT Total, and Stop. Right, the procedure AddBonus(Points : INTEGER): a terminator AddBonus, a rectangle Bonus ← DIV(Points, 10), a rectangle Total ← Total + Points + Bonus, and a terminator Return. A dashed arrow labelled call goes from the first subroutine box to the procedure, and a dashed arrow labelled back returns to the next box of the main program.

In a flowchart a subroutine call has its own symbol: a rectangle with a double line at each side. The picture shows the main program calling AddBonus twice. Each time, control goes to the procedure and comes back.

Local and global variables

Where a variable is declared decides where it can be used.

  • A global variable is declared in the main program. Every part of the program can use it.
  • A local variable is declared inside a procedure or a function. It exists only while that subroutine runs, and no other part of the program can see it. A parameter is local too.
pseudocode
DECLARE Total : INTEGER

PROCEDURE AddBonus(Points : INTEGER)
    DECLARE Bonus : INTEGER
    Bonus ← DIV(Points, 10)
    Total ← Total + Points + Bonus
ENDPROCEDURE

Total ← 0
CALL AddBonus(45)
CALL AddBonus(30)
OUTPUT Total

Total is global, so the procedure can add to it. Bonus is local: it is made fresh on each call and is gone afterwards. The first call adds 45+4=4945 + 4 = 4945+4=49, the second adds 30+3=3330 + 3 = 3330+3=33, so the output is 82. A line OUTPUT Bonus in the main program would be an error, because Bonus does not exist there.

Local variables keep a subroutine separate from the rest of the program. Two subroutines can both use a local Count without changing each other's value. That makes each one easier to test and reuse.

Python is a little different. Inside a def, a variable that is given a value is local, unless you write global total first.

Practise with new numbers

Try it in Python

The program has a procedure, a function and a procedure that changes a global variable, all in Python.

Read it first. Then run it. Do the total and the sum of squares match the pseudocode?

1. Delete the line global total and run it again. What error does Python give, and why?

2. Add print(bonus) as the last line. What happens, and what does it tell you about local variables?

Common mistakes

  • CALL with a function. A function is used in an expression: Result ← SumSquare(3, 4), never CALL SumSquare(3, 4).
  • A function with no RETURN. Every function must give back one value of the type after RETURNS.
  • Arguments in the wrong order. They are matched to the parameters by position.
  • Using a local variable outside its subroutine. It does not exist there.
  • Forgetting the data type of a parameter. Write Size : INTEGER, not just Size.

Remember these

  • PROCEDURE Name(Param : TYPE) ... ENDPROCEDURE, run with CALL Name(value).
  • FUNCTION Name(Param : TYPE) RETURNS TYPE ... RETURN value ... ENDFUNCTION, used inside an expression.
  • At most three parameters. Arguments match parameters in order.
  • Local: declared inside a subroutine, seen only there. Global: declared in the main program, seen everywhere.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. What is the difference between a procedure and a function?
  1. Find the mistake: CALL SumSquare(2, 3).
  1. In the bonus program, what is output if the two calls are CALL AddBonus(28) and CALL AddBonus(9)?
  1. Write the header of a function Average that takes three integers and returns a real value.
  1. Why is it good that Bonus is local?

Enroll free in Procedures, Functions and Strings in Pseudocode

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A program draws borders of stars with a procedure…Easy · High School (9–10) · by StemPy Lab
A shop's program works out sale prices in dollars…Medium · High School (9–10) · by StemPy Lab
Read this program carefully. The procedure declares its own Score…Hard · High School (9–10) · by StemPy Lab
Next: lesson 2Library routines and string handling
← Procedures, Functions and Strings in Pseudocode
← PreviousLibrary routines and string handling →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

Python Labs (1)

Functions with parametersPython Lab · by StemPy Lab