PDA

View Full Version : A tutorial for Parameter types



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 ;)

Rezozo
09-29-2012, 12:40 PM
It is TuTs like these that make me want to script again! Very nicely done. Simple and to the point.

Mark
09-29-2012, 12:53 PM
i agree with ^ good work nicely explained.

masterBB
09-29-2012, 12:55 PM
The difference between a const and value parameter is not noticeable in pascalscript. Both will accept changes in the function but will remain their initial value outside of the function since only the value is passed, and not the var or const itself.

var and out have in common that they are exactly the same in pascalscript. Both will use a pointer to the memory of the var instead of the value. Therefor it is useful for returning data with a procedure or function.

In Lape the const and value have a different meaning. The content of const can't be changed and the content of value can.

Will work in both pascalscript and lape though will not return a value.
procedure multi2(a: Integer);
begin
a := a * 2;
end;

Will work in pascalscript though not return a value, will error in lape cause a const value can't be changed.
procedure multi2(const a: Integer);
begin
a := a * 2;
end;

Will work in both lape and pascalscript.
procedure multi2(var a: Integer);
begin
a := a * 2;
end;

Is interpreted the same as the above example.
procedure multi2(out a: Integer);
begin
a := a * 2;
end;

In my opinion a const should not have its value changed. Even though pascalscript doesn't give an error, it is bad practice.

riwu
09-30-2012, 12:15 AM
The difference between a const and value parameter is not noticeable in pascalscript. Both will accept changes in the function but will remain their initial value outside of the function since only the value is passed, and not the var or const itself.

var and out have in common that they are exactly the same in pascalscript. Both will use a pointer to the memory of the var instead of the value. Therefor it is useful for returning data with a procedure or function.

In Lape the const and value have a different meaning. The content of const can't be changed and the content of value can.

Will work in both pascalscript and lape though will not return a value.
procedure multi2(a: Integer);
begin
a := a * 2;
end;

Will work in pascalscript though not return a value, will error in lape cause a const value can't be changed.
procedure multi2(const a: Integer);
begin
a := a * 2;
end;

Will work in both lape and pascalscript.
procedure multi2(var a: Integer);
begin
a := a * 2;
end;

Is interpreted the same as the above example.
procedure multi2(out a: Integer);
begin
a := a * 2;
end;

In my opinion a const should not have its value changed. Even though pascalscript doesn't give an error, it is bad practice.
Thanks for the explanation! I presume Const still differs from value in that it uses reference rather than duplicating? (hence useful to save memory for arguments of large size)

BraK
09-30-2012, 12:42 AM
Nice tutorial ;)

http://villavu.com/forum/showthread.php?t=59061

riwu
09-30-2012, 01:00 AM
Nice tutorial ;)

http://villavu.com/forum/showthread.php?t=59061
Ah didn't see your tutorial (the title is so vague :p)! I even browse through those AIO tutorials by Coh3n/Trilez to check if there is already a section on parameter types before making this. Nonetheless having more tutorials are always better ;)

We both have the initial misconception that Out discard initial values though, i only realize that it's behaving the same as Var for PascalScript when i tested it on Simba.

BraK
09-30-2012, 01:01 AM
Yea I left it the same for programming sake I think.

That was forever ago lol.

Daniel
09-30-2012, 03:18 AM
A nice little table :)

No prefix = Read-Only
Const = Read-Only
Var = Read/Write
Out = Write-Only