Procedures
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
A PROCEDURE is a named block
- A procedure is a named set of steps you run with
CALL. - Define it once, then call it as many times as you like.
PROCEDURE Greet
OUTPUT "Hello!"
ENDPROCEDURE
CALL Greet
CALL Greet // → Hello! (twice)
Parameters pass data in
- Put a parameter in brackets to send a value in.
PROCEDURE Greet(Name : STRING)
OUTPUT "Hi, ", Name
ENDPROCEDURE
CALL Greet("Ada") // → Hi, Ada
Explore
CALL runs it, then comes back
CALL jumps into the procedure, runs its steps, then returns to the next line.
BYREF changes the caller's variable
- A normal parameter is a copy. A
BYREFparameter is the real variable. - So a
BYREFprocedure can change the value you passed in.
PROCEDURE Double(BYREF n : INTEGER)
n ← n * 2
ENDPROCEDURE
X ← 5
CALL Double(X)
OUTPUT X // → 10
Watch out
- You must write
CALLbefore a procedure's name. - Without
BYREF, the procedure only changes its own copy — the caller's variable is untouched.
Common mistakes
PROCEDURE name(...) ... ENDPROCEDURE; run it withCALL name(...).- A procedure does a job but returns no value.
Now you try
- Define a procedure, then
CALLit. - Press Check answer to test it.
Define a PROCEDURE Greet that outputs Hello!, then CALL it twice (so Hello! prints on two lines).
Click Run to see the output here.
Define PROCEDURE Greet(Name : STRING) that outputs Hi, then the name, then CALL Greet("Ada"). Output should be Hi, Ada.
Click Run to see the output here.
Define PROCEDURE Double(BYREF n : INTEGER) that doubles n. Set X to 5, CALL Double(X), then output X (it should be 10).
Click Run to see the output here.