Zbex procedures
| Previous chapter Functions |
Zbex Manual |
Next Chapter Tables |
A procedure is a section of code which can be entered (called) from different points in a program and which, when completed, will return control to the calling point. Procedures may call other procedures, but Zbex is not designed for recursive procedure calls (that is when a procedure calls itself). The codes for procedures must come after the main program.
A procedure is identified by the procedure statement. A procedure is called using the perform statement. Return from a procedure is accomplished with the return statement. Example:
Program
---------------------
int i,j
loop for i = 1 to 5
perform square
putc ~i ~j
repeat
stop
procedure square
j = i * i
return
run
Execution
-------------------------------------
** S=10, P=29, L=235, M=410 **
1 1
2 4
3 9
4 16
5 25
Ready for program
It is possible to declare variables inside a procedure. Variables so declared can be referenced only within the procedure. If a variable of the same name was declared in the main program, it will be masked (unaccessable) inside the procedure. Example, using the variable i:
Program
---------------------
int i,j,k
loop for i = 1 to 5
j = i
perform factorial
putc ~i ~k
repeat
stop
procedure factorial
int i
k = 1
loop for i = 1 to j
k = k * i
repeat
return
run
Execution
-------------------------------------
** S=15, P=41, L=239, M=410 **
1 1
2 2
3 6
4 24
5 120
Ready for program
It is possible to pass data to a procedure and for a procedure to pass data back to the calling point. The buffers for this must be included in the procedure statement, and the data types for the buffers must be declared inside the procedure. Values must be loaded into the buffer at the time the procedure is called. The actual transfer of data is handled by the getvalue and passback statements inside the procedure. Example:
Program
---------------------
int i,j
loop for i = 6 to 10
perform factorial (i,j)
putc .w2 ~i .t4w8 ~j
repeat
stop
procedure factorial (a,b)
int a,b,i
getvalue a
b = 1
loop for i = 1 to a
b = b * i
repeat
passback b
return
run
Execution
-------------------------------------
** S=16, P=53, L=241, M=410 **
6 720
7 5040
8 40320
9 362880
10 3628800
Ready for program
There are times when you will want to return directly to a point in the main program, irrespective of how many levels of procedure calls you have made. An example of this would be a language compiler. If you are processing a line of text and a procedure within a procedure within a procedure identifies a syntax error, you would not want to trace your way back to the main program through these procedures; instead, you would want to jump directly to the error handling portion of the program with a flag telling that portion what went wrong. Zbex provides you a way to do this using the special trp variable and the special trap label. If you include an integer number with a return statement, the record of procedure calls will be erased and control will pass directly to the trap label in the main program. The special integer variable trp will be assigned the value attached to the return statement. Example:
Program
---------------------
int i
trp = 0
trap:
i = 0
perform pro1
stop
procedure pro1
putc pro1 ...
i = i + 1
if i > trp
putc
return 1
end
perform pro2
putc returning from pro1
return
procedure pro2
putc pro2 ...
i = i + 1
if i > trp
putc
return 2
end
perform pro3
putc returning from pro2
return
procedure pro3
putc pro3
putc returning from pro3
return
run
Execution
-------------------------------------
** S=31, P=83, L=233, M=410 **
pro1
pro1 pro2
pro1 pro2 pro3
returning from pro3
returning from pro2
returning from pro1
Ready for program
There are only two ways to interrupt an Zbex program once it has been started. One of these is by pressing Ctrl+Break (the other is by responding to a getc with two exclaimation points (!!)). On occasion you may want Ctrl+Break to actually do something in your Zbex program. For example, if your program is writing to the screen, and you want it to stop, you must push Ctrl+Break. In some cases, you may not want your Zbex program to halt, you may simply want to get control of it. Zbex provides you a means for doing this. It is a special procedure called break. If you include a procedure with this name, control will automatically pass to it when you press Ctrl+Break. A normal return from this procedure will put you back into your Zbex program exactly at the point where Ctrl+Break was pressed. Example:
Program
---------------------
int i,j
loop for i = 1 to 100000
loop for j = 1 to 300000 /* (for a delay)
repeat
putc ~i
repeat
stop
procedure break
getc
return
run
Execution
-------------------------------------
** S=11, P=26, L=235, M=410 **
1
2
3
(here, Ctrl+Break was pressed and a blank line entered)
4
5
!! (here, Ctrl+Break was pressed and !! was entered)
Ready for program
There is an important precaution you must observe with the procedure break. You need to provide some mechanism for terminating the program inside the procedure, or else you may not be able to terminate your program at all (except by exiting Dmuse)! The program below, once started, cannot be stopped, except by exiting Dmuse.
int i,j
loop
i = i + 1
putc ~i
repeat
stop
procedure break
return
run
| Previous chapter Functions |
Zbex Manual |
Next Chapter Tables |