Tutorial
PL/i Programming Language for i.CanDoIt
This set of pages will provide brief tutorials about programming in PL/i. This language is more simple than C, but more structured than Basic. The objective in creating this language was to compile compact code that would run efficiently and safely as a virtual machine, like a very lean Java.
Index of PL/i Tutorials
Tutorial #6

User Defined Procedures (aka Functions, Subroutines)

The definition of a procedure follows the same general structure as that of the program overall. It begins with "procedure" followed by the procedure name, followed by parameter declarations. This is followed by the "declare" block identifying local types and variables, followed by the begin..end; block of code for the procedure.

procedure myProc (a: int; b: int; var c: int)
declare
begin
<statement> ;
end ;

The parameters (variables) known locally to the procedure as "a" and "b" in the above example are passed by value, and cannot be returned to the calling program. Any parameter declared with "var" in front of it will be a call by reference, and that variable will be returned to the calling program. A procedure can have no parameters, or any number of call by value or call by reference parameters. The fact that multiple "var" or call by reference parameters are allowed means a procedure (or function) can have multiple return values. This is as close as PL/i gets to use of pointers.

To invoke the procedure, you would simply reference it by name in a statement. To call a procedure with no parameters, simply name the procedure. To call a procedure with parameters, name the procedure with its parameters in parenthesis.

program myDemo
declare
x: uint;
y: uint;
z: uint;

procedure myProc (a: int; b: int; var c: int)
declare
begin
c = a + b;
end;

procedure yourProc
declare
begin
z = 0;
end;

begin
x = geti (100);
y = geti (101);
if x > y then myProc (x, y, z);
else yourProc();
endif;
seti (100, z);
end

The result returned by "myProc" comes back in variable "z". The procedure "yourProc" does not have any parameters. Note that the parenthesis are omitted from the declaration but included as an empty set in the call to the procedure.