A procedure is a component of a computer program that allows the ugBASIC programmer to
tackle one aspect of the program at a time, without becoming distracted or side-tracked by other
programming considerations. Procedures can be thought of as programming modules, each with a
specific purpose and sphere of operation. This section explains how procedures are created and
fully exploited.
create
in / out
variables
parameters
sharing
return
A procedure is created in exactly the same way as a normal variable, by
giving it a name. The name is prefixed by PROCEDURE
keyword,
then followed by a list of parameters and the procedure itself must be ended
with an END PROC
command (or END PROCEDURE
, as well).
PROCEDURE
and END PROC
commands should be placed on their
own individual lines, but it is not mandatory.
For example:
PROCEDURE hello
PRINT "HELLO I AM A PROCEDURE!"
END PROC
If you try and run that program, nothing will happen. This is because a procedure
must be called up by name from inside your program before it can do anything.
Now add the following line at the end of that last example, compile and then run it.
PROCEDURE hello
PRINT "HELLO I AM A PROCEDURE!"
END PROC
hello[]
Another way to identify a procedure is to precede it with a PROC
statement.
Run the following example:
PROCEDURE hello
PRINT "HEY!"
END PROC
PROC hello
hello[]
CALL hello
It is possible to place the procedure definition anywhere in your program. When
ugBASIC encounters a procedure statement, the procedure is
recognised and a jump is made to the final END PROC
. In this way,
there is no risk of executing your procedure by accident.
You should be familiar with the use of ON
for jumping to a
GOSUB
routine. It is just as simple to use this structure with
procedures. In this case, if a variable holds a particular value, a system
is automatically triggered that forces a jump to a named procedure.
Of course you can have as many values triggering off as many jumps to
different procedures as you want.
For example:
ON v PROC procedure1, procedure2
Which is exactly the same as saying:
IF v = 1 THEN
procedure1[]
ELSE IF v = 2 THEN
procedure2[]
ENDIF
Normally, procedures will only return to the main program when the END PROC
instruction is reached. But supposing you need to jump out of a procedure instantly.
The POP PROC
instruction provides you with a fast getaway, if you ever find
yourself in need of escape. Try this:
PROCEDURE escape
FOR prison = 1 TO 1000000000
IF prison = 10 THEN POP PROC
PRINT "I AM ABANDONED."
NEXT
END PROC
The same result can be obtained by using the EXIT PROC
command, with the
difference that EXIT PROC
can be extended by using an expression, in order
to exit from the procedure on a specific condition:
PROCEDURE escape
FOR prison = 1 TO 1000000000
EXIT PROC IF prison == 10
PRINT "I AM ABANDONED."
NEXT
END PROC
To better understand the mechanisms of operation of variables within procedures,
it is necessary to understand that the procedures on ugBASIC are
“stackless”. It means that it is possible to define a variable local to the
function, therefore invisible to the main program or to other functions, but
always visible to the function if it is called on itself, and reinitialized.
So, all of the variables that are defined inside a procedure work completely
separately from any other variables in your programs. We call these variables
"local" to the procedure. All local variables are automatically initialized
when procedure has started executing, and they maintain their values from
execution to execution. So that in the following example the values
1, 2 and 3 will be printed:
PROCEDURE plus
a = a + 1
PRINT a
END PROC
plus[]
plus[]
plus[]
All the variables outside of procedures are known as “global” variables, and they
are not affected by any instructions inside a procedure. So it is perfectly possible
to have the same variable name referring to different variables, depending on whether
or not they are local or global. When the next example is run, it can be seen that
the values given to the global variables are different to those of the local variables,
even though they have the same name. Because the global variables cannot be accessed
from inside the procedure, the procedure assigns a value of zero to them no mater
what value they are given globally.
a = 42
b = 84
PROCEDURE example
PRINT a
PRINT b
END PROC
example[]
To avoid errors, you must treat procedures as separate programs with their own sets
of variables and instructions. So it is very bad practice for the ugBASIC
programmer to use the same variable names inside and outside a procedure, because you
might well be confused into believing that completely different variables were the same,
and tracking down mistakes would become a nightmare. To make life easy, there are simple
methods to overcome such problems.
One method is to define a list of parameters in a procedure. This creates a group
of local variables that can be loaded directly from the main program. For example:
PROCEDURE hello [name$]
PRINT "HELLO " + name$
END PROC
hello["WORLD"]
hello["NICE TO MEET YOU!"]
Note that the values to be loaded into name$
are entered between square
brackets as part of the procedure call. This system works equally well with constants
as well as variables, but although you are allowed to transfer integer, real or string
variables, you may transfer also arrays using this method. If you need to enter more
than one parameter, the variables must be separated by commas, like this:
PROCEDURE twins[a,b]
PRINT a + b
END PROC
PROCEDURE triplets[ x$, y$, z$ ]
PRINT x$ + y$ + z$
END PROC
hello["WORLD"]
hello["NICE TO MEET YOU!"]
Those procedures could be called like this:
twins [6,9]
triplets ["XENON","YAK","ZYGOTE"]