Procedures — Pseudocode

PROCEDURE name(parameter) ... END PROCEDURE makes a procedure: a few steps that are given one name. name(...) calls it, which means it runs those steps…

In the robot courier game, the robot knows only three commands: move one square, turn left, and deliver the water jug. There is no turn right. So how does it turn right?

Try it with your own body: turn left, turn left, turn left. You now face to the right of where you started. So one right turn is the same as three left turns. The problem is that writing the same three lines at every corner is tiring, and it is easy to miscount and write two or four.

It would be easier to give those three lines one name, such as "turn right", and then call that name each time you need them. You can. Steps that are given one name are called a procedure.

PROCEDURE: steps with a name

The robot's commands only run inside its game, so the examples in this lesson use the receipt from Mrs. Sri's stall. The receipt needs a dividing line above and below the stall's name. This program prints it, and the result is below the code:

PROCEDURE divider()
    PRINT "--------------------"
END PROCEDURE

divider()
PRINT "Mrs. Sri's Stall"
divider()
--------------------
Mrs. Sri's Stall
--------------------

PROCEDURE divider() makes a procedure named divider. The brackets are empty because this procedure needs no values. The body of the procedure only runs when you call it by writing divider(). This program calls it twice, so the dividing line is printed twice, although its PRINT line is written only once. To change the dividing line to ====, you change just one line of code.

The turn-right procedure for the robot courier is made the same way. Its body is three left turns, and the program just calls its name each time the robot has to turn right.

Parameters: same steps, different contents

Like a function, a procedure can have parameters. The steps stay the same, but the printed text follows the values you send, as in this program:

PROCEDURE callOut(name, order)
    PRINT "Order for", name, "ready:", order
END PROCEDURE

callOut("Sari", "iced tea")
callOut("Dodi", "fried rice")
Order for Sari ready: iced tea
Order for Dodi ready: fried rice

PROCEDURE or FUNCTION?

The difference is the result: a function hands back a value, and a procedure does not.

FUNCTION PROCEDURE
Hands back a value? yes, through RETURN no
How it's called where a value is needed: total ← subtotal(4, 2) on its own line: divider()
Good for calculating doing steps: printing a receipt, moving a robot

Choosing is easy. If you need a number or text to use again, make a function. If you only need the steps to be done, make a procedure.

Functions and procedures are often used together. In this program, the procedure row prints one line of the receipt, and calls the function subtotal to calculate the price:

FUNCTION subtotal(price, count)
    RETURN price * count
END FUNCTION

PROCEDURE row(item, price, count)
    PRINT item, "x", count, "=", subtotal(price, count)
END PROCEDURE

row("Iced tea", 4, 2)
row("Coffee", 5, 1)
Iced tea x 2 = 8
Coffee x 1 = 5

Leaving early

In a procedure, RETURN without a value is used to leave the procedure before its last line. The lines below the RETURN do not run. In this program, the procedure leaves early when the stock is 0:

PROCEDURE checkStock(item, stock)
    IF stock = 0 THEN
        PRINT item, "sold out"
        RETURN
    END IF
    PRINT item, "has", stock, "left"
END PROCEDURE

checkStock("Iced tea", 0)
checkStock("Coffee", 5)
Iced tea sold out
Coffee has 5 left

When the stock of iced tea is 0, the procedure prints Iced tea sold out and then leaves, so the line below END IF does not run.

In JavaScript and Python, a procedure is written as an ordinary function with no return that carries a value.

⚠️ A procedure asked for a result

TL;DR: PROCEDURE name(parameter) ... END PROCEDURE makes a procedure: a few steps that are given one name. name(...) calls it, which means it runs those steps, whenever you need them. For example, a robot that can only turn left gets a turn-right procedure made of three left turns. A procedure does its steps and hands back no value, so you call it on its own line: there is nothing to store in a variable. If you need a result to calculate with, use FUNCTION and RETURN. In a procedure, RETURN with no value is used to leave the procedure early.

⚡ Steps with a name

Write:

PROCEDURE divider()
    PRINT "--------------------"
END PROCEDURE

divider()
PRINT "Mrs. Sri's Stall"
divider()
Guess first: how many times is the dividing line printed? The line of code that prints it is written only once, inside the procedure at the top.

Twice, once for each divider(). The lines between PROCEDURE and END PROCEDURE do not run right away, only when the procedure is called.

Meaning: PROCEDURE divider() makes a procedure: a few steps that are given one name. Writing divider() calls it, which means it runs those steps. To change the dividing line, you change just one line.

⚡ Same steps, different contents

Write:

PROCEDURE callOut(name, order)
    PRINT "Order for", name, "ready:", order
END PROCEDURE

callOut("Sari", "iced tea")
callOut("Dodi", "fried rice")

Meaning: Like a function, a procedure can have parameters. The steps are the same in every call, but the printed text follows the values you send. The first call prints Order for Sari ready: iced tea.

⚡ Procedure or function?

Guess first: callOut is a procedure. Can you store its result with x ← callOut("Sari", "iced tea")?

No. The program stops with the message: callOut is a PROCEDURE, so it gives no value.

Meaning: A function hands back a value through RETURN, so it is good for calculating. A procedure only does steps, such as printing a receipt, and hands back no value. That is why you call it on its own line.

What you will learn