Application Center - Maplesoft

App Preview:

Maple Programming: 4.4: Parameter, local, and global variables

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

4.04.mws

Programming in Maple

Roger Kraft
Department of Mathematics, Computer Science, and Statistics
Purdue University Calumet

roger@calumet.purdue.edu

4.4. Parameter, local, and global variables

In a procedure there are three kinds of variables, parameter variables , local variables , and global variables . Consider the following example, a slight variation on the last example from the last section.

>    plus := proc(x,y)

>         local a, b;         

>         global c;

>         a := x - y;

>         b := x * y;

>         c := x ^ y;

>              x + y;  # the return value

>    end;

plus := proc (x, y) local a, b; global c; a := x-y; b := x*y; c := x^y; x+y end proc

This procedure has two parameter variables ( x  and y ), two local variables ( a  and b ), and one global variable ( c ). The parameter variables ( i.e.,  formal parameters) are place holders for the actual inputs passed to the procedure during a procedure call. The local variables can be thought of as "scratch pad variables", or "temporary variables".  Local variables are used to hold temporary results that might come up while we are trying to compute our main result in the procedure. As we will see, the local variables only "live" inside the procedure call. They do not exist outside the procedure, and they do not survive from one procedure call to the next (this is why they get their name "local"). The global variable is just that, it is global to the whole Maple worksheet. As we will see, global variables inside procedure bodies are really the same as the variables we use in commands at our worksheet prompts.

Let us see how all this works with the procedure plus  (make sure that you have executed the definition of plus ). Here is a procedure call for plus .

>    plus( 2, 5 );

7

Now what about the variables a , b , and c ?

>    a; b; c;

a

b

32

Notice that a  and b  are still unassigned but c  has the value 2^5  = 32. The variables a  and b  in our worksheet are unassigned because the variables a  and b  inside the procedure plus  are local variables. The a  and b  inside plus  have no affect on the "global" a  and b , the ones in our worksheet. On the other hand, the "global" variable c  in our worksheet is the same variable as the c  inside plus  since it is declared to be a global variable there. Notice that a global variable can be used as a sneaky way for a procedure to "output" another result besides its official return value. Here is another call to the procedure plus .

>    plus( -2, 2 );

0

Now look at the value of c .

>    c;

4

The value of c  changes with each call to plus .

It is worth mentioning that parameter variables are a kind of local variable since they have no affect except during a procedure call. For example, notice that the variables x  and y  are still unassigned in the worksheet.

>    x; y;

x

y

The parameter variables x  and y  that were given the values -2  and 2  in the last procedure call were local to the procedure call, so they have no affect on the global x  and y  in our worksheet.

We will see several uses for local and global variables later in this chapter and in the following chapters.

>   

>