Procedures
That which can be done twice should only be done once
As programs grow, we often find ourselves writing the same code over and over. A procedure lets us give a block of code a name, so we can run it whenever we want just by using that name. Procedures are also called functions in many other languages.
We have already been using procedures. DISPLAY, LENGTH, and APPEND are all
procedures that someone else wrote for us. Now we will write our own.
- Procedure (noun)
- A reusable named section of code
Defining and Calling
We define a procedure with the PROCEDURE keyword, followed by a name we choose,
parentheses, and a block of code in curly braces.
Defining a procedure does not run it. To run it, we call it by writing its name followed by parentheses. We can call it as many times as we like.
This displays Hello! three times.
Parameters
Most procedures need information to do their job. We pass that information in through parameters, which are names listed inside the parentheses. Each parameter is a variable that holds whatever value was given when the procedure was called.
- Parameters (noun)
- A name for a value a procedure receives
The value we place in the parentheses of the call, here a list, is called an argument.
Inside the procedure, lst holds that list. This displays [ 1 2 3 4 5 6 ].
- Argument (noun)
- The actual value passed in a call
Remember that a list cannot be displayed directly. This procedure is a useful way to show one, since it loops over the items for us.
A procedure can take as many parameters as it needs. Separate them with commas. The arguments are matched to the parameters in order.
This displays 24. Here a is 1, b is 2, c is 3, and d is 4.
Returning Values
So far our procedures only display things. Often we would rather a procedure hand a
value back to us, so we can store it or use it in a bigger expression. We do this with
RETURN.
When CSP reaches RETURN, the procedure ends immediately, and the call is replaced by
the returned value. Here square(4) becomes 16, so 16 is displayed.
Because a call with a return value is just a value, we can use it anywhere a value is allowed.
This displays 25.
Any code placed after a RETURN inside the same block will never run.
Random
One useful procedure that is built into CSP is the RANDOM procedure. Random takes two
arguments and returns a random number between the two. The fist number must be less than
or equal to the second, and both must be integers.
Procedures Without a Return
A procedure that never reaches a RETURN, like printList, produces no value. We say
it returns null, which means “nothing.” Null is not a string, number, or boolean. It is
the absence of a value.
- Null (noun)
- The representation of the absence of a value
The line assigning to v is allowed, but v holds nothing. Trying to use it in the
next line causes a null value error.
Only store the result of a procedure that returns something. If a procedure only displays or changes things, call it on its own line.
Scope
Not every variable can be used everywhere. The part of a program where a variable can be used is called its scope.
- Scope (noun)
- The region of code a variable is accessible within
Parameters, and any variables created inside a procedure, are local to that procedure. They are created when the procedure is called, and they disappear when it ends. Code outside the procedure cannot see them. In order to create a global variable it must be defined outside of all blocks.
- Local (adj)
- Existing only inside one procedure or block
- Global (adj)
- Existing in all scopes
The first DISPLAY works and shows 10. The second causes an error, because answer
only existed while double was running.
Since each call gets its own fresh set of local variables, a procedure cannot accidentally damage variables elsewhere in the program. This also means two procedures can use the same name for different things without any conflict.
The x in a and the x in b are two separate variables.
The way to get information out of a procedure is RETURN, and the way to get
information in is through parameters.
Procedures are not the only place scope shows up. The curly braces of an IF or ELSE
block have their own scope too. A variable created inside a block exists only until the
block ends.
This causes an error. The variable v was created inside the IF block, so it was gone
by the time we reached DISPLAY(v). It does not matter that the condition was true
and the block ran.
To use a value after the block, create the variable before it.
This displays true. Because v already existed outside the block, the assignment
inside the block changes that same variable instead of making a new one.
This gives us a pattern we will use often. Create a variable with a starting value, let a conditional change it, and then use it afterwards.
In Other Languages
Procedures exist in nearly every programming language. Most languages call them
functions, and many require extra details such as the type of each value. See
equivalent definitions of square and m below.
In C#, each method states the type of value it returns. int means it returns an
integer, and void means it returns nothing, like our printList procedure.
Next: Input