SRL Archive - OSR Help

Passing the name of a variable to procedure


Foundry

2013-10-12 22:42:28

It's a little difficult to explain (since I'm such a noob at programming/scripting) but I am trying to make a procedure that takes the name of two global variables, manipulates them, and assigns a new value to the global var it was passed. Here's an example of what I am trying to do -

program Example;
var
Ball2, Ball3: Integer;

procedure addNStuff(var1, var2); //I want variables to be parameters
begin
var1 := manipulateAndDoStuff(var1) + var2; //And I want it to be able to return
end; //a value back to the global variable.

begin
addNStuff(Ball2, Ball3); //So I can just pass the names of the variables to the function.
end.

NKN

2013-10-12 23:02:14


procedure addNStuff(var1, var2); //I want variables to be parameters
begin
var1 := manipulateAndDoStuff(var1) + var2; //And I want it to be able to return
end;

—>
procedure addNStuff(); //I want variables to be parameters
begin
Ball2 := manipulateAndDoStuff(Ball2) + Ball3; //And I want it to be able to return
end;

?

Foundry

2013-10-12 23:11:35

From: NKN

procedure addNStuff(var1, var2); //I want variables to be parameters
begin
var1 := manipulateAndDoStuff(var1) + var2; //And I want it to be able to return
end;

—>
procedure addNStuff(); //I want variables to be parameters
begin
Ball2 := manipulateAndDoStuff(Ball2) + Ball3; //And I want it to be able to return
end;

?


Thanks, but I'm trying to pass the name of the var because I have to do the same thing to 3 global variables and it just looks sloppy to type it over.

Brandon

2013-10-13 00:20:25

From: HardcorBoter
Thanks, but I'm trying to pass the name of the var because I have to do the same thing to 3 global variables and it just looks sloppy to type it over.



The term you are looking for is called a "reference". Meaning to pass the address of a variable such that the function can modify the passed variable. Without references, it is pass by value by default and so the function makes a copy of the parameter for its own personal use.

Here is an example of how to pass a variable by reference:


Procedure manipulateAndDoStuff(var A: Integer);
Begin
A := A + Random(10);
End;

Procedure AddNStuff(var A, B: Integer);
Begin
A := manipulateAndDoStuff(A) + B;
End;

var
C, D: Integer;
begin
AddNStuff(C, D);
end.

Note the usage of the "var" keyword to pass the variables by reference..



The other way is to pass a pointer to the variable such as below:


Procedure manipulateAndDoStuff(A: ^Integer);
Begin
A^ := A^ + Random(10);
End;

Procedure AddNStuff(A, B: ^Integer);
Begin
A^ := manipulateAndDoStuff(A^) + B^;
End;

var
C, D: Integer;
begin
AddNStuff(@C, @D);
end.

Both are equivalent in this case. However, the bottom one may have more overhead due to the extra de-reference and address of. So.. you're better off with the var keyword in this case.

Foundry

2013-10-13 00:46:18

Thanks Brandon, exactly what I was looking for (plus an easy to understand explanation) :D