Skip to main content

Procedure Variables

Variables used within procedures are automatically private to that procedure, which means their scope is local to the procedure only. To share some of these variables with procedures that this procedure calls, pass them as parameters to the other procedures.

You can also declare public variables. Any procedures that declare a common set of public variables share these variables. To declare public variables, list them in square brackets following the procedure name and its parameters.

Use of public (shared) variables is no longer considered good programming practice. However, this example, and the exercises in this tutorial, use a mix of public and private variables for demonstration purposes.

SAMPLES>write

SAMPLES>d ^publicvarsexample

setting a
setting b
setting c
The sum is: 6
SAMPLES>write

a=1
b=2
SAMPLES>

The publicvarsexample.mac code:

publicvarsexample
    ; examples of public variables
    ;
    do proc1()   ; call a procedure
    quit    ; end of the main routine
    ;
proc1() [a, b]
    ; a private procedure
    ; "c" and "d" are private variables
    {
    write !, "setting a"  set a = 1
    write !, "setting b"  set b = 2
    write !, "setting c"  set c = 3
    set d = a + b + c
    write !, "The sum is: ", d
    }
FeedbackOpens in a new tab