riwu
09-29-2012, 12:29 PM
This tutorial will cover different types of parameters (namely value, Const, Out and Var)
What is a parameter?
A parameter is a kind of variable specified by the function/procedure which will be used within the procedure/function.
Unlike the output (result) of a function, which can only return 1 variable, there is no practical limit to the number of parameter variables you can have for a function/procedure.
Eg.
function FindColor(var x, y: Integer; col, x1, y1, x2, y2: Integer):Boolean;
Parameters are everything within the parenthesis that comes after the function name. In the example, x, y, col, x1, y1, x2, y2 are the parameter variables, Integer is the variable type declared, and the var front of the variable x,y is the parameter type, which will the main focus of this tutorial.
To use the function, one would have to specify all the parameters defined by the function. Eg: FindColor(x,y,ObjColor,20,0,0,100,100). The input values of the parameter are known as ‘arguments’.
Through specifying the type of parameter to be passed, the parameter can be used for inputting a value to the function, as well as returning values from the function back to the parameters. In this case, x,y, being specified as ‘var’, will be stored values gathered within the function. (will be elaborated later on)
Parameter Types
Value parameter
This is the default parameter type if it is not defined (hence the most commonly used parameter type). Back to the example above, col, x1, y1, x2, y2 are all value parameters as they are not assigned as any type (i.e. nothing is in front of the variable names).
A value parameter can only pass values to the function/procedure, and global variables altered within the function/procedure will remain unchanged outside the function/procedure.
program new;
var
MyNumber: Integer;
function Add1(Input: Integer): Integer;
begin
Result:=Input + 1; //using the argument to return a result
Input:=10; //attempting to change the argument (which will fail)
end;
begin
MyNumber:=5;
writeln('MyNumber+1 = '+ToStr(Add1(MyNumber)));
writeln('Final Number = ' + ToStr(MyNumber)); //recognize that it remain unaltered
end.
Const parameter
Const parameter is very similar to value parameter, as both can use the arguments within the function, but it's value outside the function cannot be altered. In addition to that, for a Const parameter, as the name suggests, one should never alter it (through assignment). Attempting to do so may cause compiling errors.
Another key distinction is that value arguments are passed by value (i.e. a copy of them will be created and only the copy will be used for the function, which is also why the original variable cannot be altered within the function), whereas Const arguments are passed via reference and original variables are not duplicated.
As such, Const parameters are often only used when the arguments are likely to be large. (Eg. strings, TPA/ATPA)
A typical example:
function DTMFromString(const DTMString: String): Integer;
Out Parameter
Out parameter is the exact complement of Const parameter. It does not accept any value arguments (they are set to null on entry) but they can return values to the argument, hence global arguments can be altered within the function/procedure.
Var Parameter
This is the second most commonly used parameter type. Var parameter is a combination of the value/Const parameter and the Out parameter. It can accept initial values, as well as returning output values to the parameter.
program new;
var
MyNumber: Integer;
procedure Add1(var VarNum: Integer);
begin
VarNum:=VarNum + 1; //directly altering the initial value and return the new value to the same parameter
end;
begin
MyNumber:=5;
Add1(MyNumber);
writeln('MyNumber+1 = '+ToStr(MyNumber));
end.
Conclusion
In multithreading environments, Const/Out parameters are crucial to prevent unintentionally altering the initial arguments. Strangely in Simba, no warning is given when attempting to reassign Const parameters, and Out does not discard initial value, making it essentially the same as Var parameter.
The difference between a const and value parameter is not noticeable in pascalscript. In Lape the const and value have a different meaning. The content of const can't be changed and the content of value can.
var and out have in common that they are exactly the same in pascalscript.
Nevertheless, it is still good practice to use them in the intended way as explained above ;)
What is a parameter?
A parameter is a kind of variable specified by the function/procedure which will be used within the procedure/function.
Unlike the output (result) of a function, which can only return 1 variable, there is no practical limit to the number of parameter variables you can have for a function/procedure.
Eg.
function FindColor(var x, y: Integer; col, x1, y1, x2, y2: Integer):Boolean;
Parameters are everything within the parenthesis that comes after the function name. In the example, x, y, col, x1, y1, x2, y2 are the parameter variables, Integer is the variable type declared, and the var front of the variable x,y is the parameter type, which will the main focus of this tutorial.
To use the function, one would have to specify all the parameters defined by the function. Eg: FindColor(x,y,ObjColor,20,0,0,100,100). The input values of the parameter are known as ‘arguments’.
Through specifying the type of parameter to be passed, the parameter can be used for inputting a value to the function, as well as returning values from the function back to the parameters. In this case, x,y, being specified as ‘var’, will be stored values gathered within the function. (will be elaborated later on)
Parameter Types
Value parameter
This is the default parameter type if it is not defined (hence the most commonly used parameter type). Back to the example above, col, x1, y1, x2, y2 are all value parameters as they are not assigned as any type (i.e. nothing is in front of the variable names).
A value parameter can only pass values to the function/procedure, and global variables altered within the function/procedure will remain unchanged outside the function/procedure.
program new;
var
MyNumber: Integer;
function Add1(Input: Integer): Integer;
begin
Result:=Input + 1; //using the argument to return a result
Input:=10; //attempting to change the argument (which will fail)
end;
begin
MyNumber:=5;
writeln('MyNumber+1 = '+ToStr(Add1(MyNumber)));
writeln('Final Number = ' + ToStr(MyNumber)); //recognize that it remain unaltered
end.
Const parameter
Const parameter is very similar to value parameter, as both can use the arguments within the function, but it's value outside the function cannot be altered. In addition to that, for a Const parameter, as the name suggests, one should never alter it (through assignment). Attempting to do so may cause compiling errors.
Another key distinction is that value arguments are passed by value (i.e. a copy of them will be created and only the copy will be used for the function, which is also why the original variable cannot be altered within the function), whereas Const arguments are passed via reference and original variables are not duplicated.
As such, Const parameters are often only used when the arguments are likely to be large. (Eg. strings, TPA/ATPA)
A typical example:
function DTMFromString(const DTMString: String): Integer;
Out Parameter
Out parameter is the exact complement of Const parameter. It does not accept any value arguments (they are set to null on entry) but they can return values to the argument, hence global arguments can be altered within the function/procedure.
Var Parameter
This is the second most commonly used parameter type. Var parameter is a combination of the value/Const parameter and the Out parameter. It can accept initial values, as well as returning output values to the parameter.
program new;
var
MyNumber: Integer;
procedure Add1(var VarNum: Integer);
begin
VarNum:=VarNum + 1; //directly altering the initial value and return the new value to the same parameter
end;
begin
MyNumber:=5;
Add1(MyNumber);
writeln('MyNumber+1 = '+ToStr(MyNumber));
end.
Conclusion
In multithreading environments, Const/Out parameters are crucial to prevent unintentionally altering the initial arguments. Strangely in Simba, no warning is given when attempting to reassign Const parameters, and Out does not discard initial value, making it essentially the same as Var parameter.
The difference between a const and value parameter is not noticeable in pascalscript. In Lape the const and value have a different meaning. The content of const can't be changed and the content of value can.
var and out have in common that they are exactly the same in pascalscript.
Nevertheless, it is still good practice to use them in the intended way as explained above ;)