Duration of Local Variables

A local variable is valid for the duration of the PeopleCode program or function in which it is defined. A PeopleCode program is defined as what the PeopleCode Editor in Application Designer presents in a single window: a chunk of PeopleCode text associated with a single item (a record field event, a component record event, and so on.)

When the system evaluates a PeopleCode program and calls a function in the same PeopleCode program, a new program evaluation is not started.

However, when a function from a different PeopleCode program is called (that is, some PeopleCode text associated with a different item), the current PeopleCode program is suspended, and the Component Processor starts evaluating the new program. This means that any local variables in the calling program (called A) are no longer available. Those in the called program (called B) are available.

Even if the local variables in the A program have the same name as those in the B program, they are different variables and are stored separately.

If the called program (B) in turn calls a function in program A, a new set of program A's variables are allocated, and the called function in A uses these new variables. Thus, this second use of program A gets another lifetime, until execution returns to program B.

The following is an example of pseudocode to show how this might work. (This is non-compiled, non-working code. To use this example, you'd have to enter a similar program without the external declaration of the function in the other, not yet compiled, one.)

Program A (Rec.Field.FieldChange): 
local number &temp; 
declare function B1 PeopleCode Rec.Field FieldFormula; 
/* Uncomment this declaration and comment above to compile this the first time. 
   function B1 
   end-function; 
*/ 
function A1 
WinMessage("A1: &temp is " | &temp); 
&temp = &temp + 1; 
A2(); 
B1(); 
A2(); 
end-function; 
function A2 
WinMessage("A2: &temp is " | &temp); 
&temp = &temp + 1; 
end-function; 
A1(); 
Program B (Rec.Field.FieldFormula): 
local number &temp; 
declare function A2 PeopleCode Rec.Field FieldChange; 
function B1 
WinMessage("B1: &temp is " | &temp); 
&temp = &temp + 1; 
A2(); 
end-function;

When this is compiled and run, it produces the following output:

A1: &temp is 0
A2: &temp is 1
B1: &temp is 0
A2: &temp is 0
A2: &temp is 2