2 call statement, Call statement – Rice Lake iRite IDE User Manual

Page 26

Advertising
background image

22

920i

Programming Reference

rTotalCost := ((iNumBolt * rBoltPrice) + (iNumNuts * rNutPrice)) * (1 + rTaxRate);

sOutputText := The total cost is : " + RealToString(rTotalCost, 4, 2) + " dollars.";

Assignment of different but compatible types:

iValue := 34.867; -- Loss of significant digits! iValue will equal 34, no rounding!

rDegrees := 212; -- No problem! rDegrees will equal 212.000000000000000000

3.4.2

Call Statement

The call statement is used to initiate a subprogram invocation. The number and type of any actual parameters are
compared against the number and type of the formal parameters that were defined in the subprogram declaration.
The number of parameters must match exactly. The types of the actual and formal parameters must also be
compatible. Parameter passing is accomplished by copy-in, or by copy-in/copy-out for var parameters.

<call-stmt>:

<name> ';'

;

Copy-in refers to the way value parameters are copied into their corresponding formal parameters. The default
way to pass a parameter in

iRite

is “by value”. By value means that a copy of actual parameter is made to use in

the function or procedure. The copy may be changed inside the function or procedure but these changes will
never affect the value of the actual parameter outside of the function or procedure, since only the copy may be
changed.

The other way to pass a parameter is to use a copy-in/copy-out method. To specify the copy-in/copy-out method,
a formal parameter must be preceded by the keyword var in the subprogram declaration. Var stands for
“variable”, which means the parameter may be changed. Just like with a value parameter, a copy is made.
However, when the function or procedure is done executing, the value of the copy is then copied, or assigned,
back into the actual parameter. This is the copy-out part. The result is that if the formal var parameter was
changed within the subprogram, then the actual parameter will also be changed after the subprogram returns.
Actual var parameters must be values: a constant cannot be passed as a var parameter.

One potentially troublesome issue occurs when passing a global parameter as a var parameter. If a global
parameter is passed to a function or procedure as a var parameter, then the system makes a copy of it to use in the
function body. Let’s say that the value of the formal parameter is changed and then some other function or
procedure call is made after the change to the formal parameter. If the function or procedure called uses, by
name, the same global parameter that was passed into the original function, then the value of the global
parameter in the second function will be the value of the global when it was pass into the original function. This
is because the changes made to the formal parameter (only a copy of the actual parameter passed in) have not yet
been copied-out, since the function or procedure has not returned yet. This is better demonstrated with an
example:

program GlobalAsVar;

g_ciPrinterPort : constant integer := 2;

g_sString : string := "Initialized, not changed yet";

procedure PrintGlobalString;
begin
WriteLn(g_ciPrinterPort, g_sString);
end;


procedure SetGlobalString (var vsStringCopy : string);
begin

vsStringCopy := "String has been changed";

Write(g_ciPrinterPort, "In function call: ");
PrintGlobalString;

Advertising
This manual is related to the following products: