Sub topic 5
Procedures, Parameters, and Return Values
This chapter assumes you already know how functions work in at least one programming language and can read the assignment, selection, and loop constructs from earlier chapters. Here you will learn to package steps into named units in pseudocode, so that larger algorithms stay readable and each part can be traced on its own.
Functions and Procedures
Pseudocode usually separates two kinds of named blocks:
| Kind | Produces a value? | Typical use |
|---|---|---|
| `FUNCTION` | Yes, through `RETURN` | Compute a result, such as a maximum or a sum |
| `PROCEDURE` | No | Perform an action, such as sorting an array in place or printing |
Use the keyword that matches the block's job. A reader who sees PROCEDURE knows not to look for a result, and a reader who sees FUNCTION knows every path must end in RETURN.
FUNCTION FindMax(A, n)
max ← A[1]
FOR i ← 2 TO n DO
IF A[i] > max THEN
max ← A[i]
END IF
END FOR
RETURN max
END FUNCTIONThe header names the block and lists its parameters, A and n. Parameters are placeholders. When a caller writes biggest ← FindMax(scores, 10), the arguments scores and 10 are bound to A and n for that one call.
How a Call Flows
RETURN does two things. It hands back a value, and it ends the function right away. Any lines after an executed RETURN are skipped.
Passing Parameters: State Your Convention
Pseudocode has no compiler to settle how arguments are passed, so you have to say. A common convention, and the one this tutorial uses:
- Simple values such as numbers and booleans are passed by value. The callee gets a copy, so changing it inside the callee does not affect the caller.
- Arrays are passed by reference. The callee works on the caller's array, so changes are visible after the call.
That convention is what lets a procedure like InsertionSort(A, n) sort an array without returning anything. If your algorithm depends on a different rule, write it in a comment above the header.
Common Errors
- A missing return path. If one branch of an
IFreturns and the other falls off the end, the result for that branch is undefined. - Using a local variable outside its block. Variables assigned inside a procedure, such as
maxabove, are local. The caller cannot see them and must use the returned value. - Argument order.
FindMax(10, scores)bindsA ← 10. Order matters unless your pseudocode uses named parameters. - Forgetting the result. A bare
FindMax(scores, 10)line computes the maximum and then throws it away. Assign the result or use it in an expression.
Exercise
A procedure is written as PROCEDURE Double(x) x ← x × 2 END PROCEDURE. The caller runs k ← 5, then Double(k), then prints k. Under this chapter's convention, what is printed, and how would you rewrite Double so the caller gets 10?
Show answer
It prints 5, because x is a number passed by value and doubling the copy leaves k unchanged. Rewrite it as FUNCTION Double(x) RETURN x × 2 END FUNCTION and call it as k ← Double(k).
Exercise
What is wrong with FUNCTION Sign(x): IF x > 0 THEN RETURN 1 ELSE IF x < 0 THEN RETURN -1 END IF END FUNCTION?
Show answer
When x = 0 no RETURN runs, so the function produces no value. Add RETURN 0 before END FUNCTION, or add an ELSE branch that returns 0.