The actual instructions that make up a subroutine or a function
can be identical. It is the way you want to use them in
a program that
turns them into either a subroutine or a function. For example, you
can call the built-in function SUBSTR as either a function or a subroutine.
This is how to call SUBSTR as a function to shorten a word to its
first eight characters:
a = SUBSTR('verylongword',1,8) /* a is set to 'verylong' */
You get the same results if you call SUBSTR as a subroutine.
CALL SUBSTR 'verylongword', 1, 8
a = RESULT /* a is set to 'verylong' */
When deciding whether to write a subroutine or a function, ask
yourself the following questions:
- Is a returned value optional? If so, write a subroutine.
- Do I need a value returned as an expression within an instruction?
If so, write a function.
The rest of this chapter describes how to write subroutines and
functions and finally summarizes the differences and similarities
between the two.