Parameters Or Arguments?
The Parameter is the temporary variable name within a Function or Procedure sub-routine, while the Argument is the value contained within or passed in by this temporary variable.
So Parameters (or Arguments) are the input values that are passed within parentheses into either a function or into a procedure.
While a Parameter is a placeholder for an Argument, the two terms are sometimes used interchangeably to refer to the same thing.
A Function or Procedure can contain one or more Parameters - where there is more than one, it is a Parameter List, which is simply a series of Parameter declarations inside a set of parentheses that are separated by a semi-colon (;). This list specifies the number, type, and order of Parameters that must be passed to the routine when it is called.
Not all sub-routines will require a Parameter to be passed in. Both Functions and Procedures can be defined to operate without any data values being passed in such as a Function that simply returns a randomly generated number. In these cases, simply omit the parentheses and all other details as shown below.
procedure
DoIt;
begin
LogInfo(
'Doing it!!!'
);
end
;
There are several different types of Parameter. Every Parameter has a classification - by default this is value, but they can also be variable, constant, or out. Variable, constant, and out are indicated by use of the reserved keywords var, const and out. The default value Parameters are always given a type, whereas Variable, constant, and out can be either typed or untyped.
Value - these Parameters have a value passed in to the routine. The routine copies the Argument value and performs whatever it needs to on the copy value - the routine is free to read and write to the copied Parameter. At completion of the routine the copy is discarded, so there is no effect on the original value. A simple example follows where XNum is the parameter in the Function called DoubleIt. XNum will contain the same value at the end of the routine as it had at the start.
function
DoubleIt(XNum :
integer
):
integer
;
Variable (var) - these get their Argument by reference. Rather than working with a copy of the value passed in, any changes made to the Argument persist and are carried out of the routine on completion. Similar to the example above, the introduction of var before the Parameter means that the resulting value contained in XNum could be different from the starting value once the function has been called and run.
function
DoubleIt(
var
XNum :
integer
):
integer
;
Constant (const) - similar to the value Parameter, a constant Parameter however cannot be assigned a value within the Function or Procedure that calls it. It is essentially a read-only Argument and should be used for types such as strings as it gives better performance. Two examples follow.
procedure
DoStuff(
const
a :
string
);
begin
LogInfo(
'Stuff='
+ a);
end
;
function
uc(
const
s1 :
string
):
string
;
begin
result := Uppercase(s1);
end
;
Out - as suggested by the name, this Parameter is for output purposes only by instructing the routine where to store output. It is similar to a variable Parameter where the Argument is passed by reference, but the routine does not expect the Parameter to have an initial value. This out Parameter type serves the same purpose of the special variable called Result.
function
GameTotal(out FinalNum :
integer
) :
integer
;
begin
FinalNum :=
25
;
RESULT := FinalNum;
end
;
procedure
ScriptEvent (
var
Value : variant);
var
outTest :
integer
;
begin
LogInfo(IntToStr(GameTotal(outTest)));
end
;