Function Parameters

When a parameter is passed to a function, the function can see the value of the parameter, but it cannot make any changes to the variable that was used - this is known as passing a parameter by value. e.g.

function half(float x)
  x = x/2;    # x is divided by two, but this has no affect on the variable d.
  echo(x);
endfunction


d = 4;
half(d);
echo(d);      # d is still 4 not 2

It is also possible to declare a parameter so that any changes a function makes to a parameter also change the variable that was passed to the function. This is called passing by reference.

In order to specify that a parameter is passed by reference it must be preceded by the & symbol e.g.

function half(float &x)
  x = x/2;    # x is divided by two and so is the variable d.
  echo(x);
endfunction


d = 4;
half(d);
echo(d);      # d is now 2 not 4

Passing parameters by reference is useful if you want a function to affect two or more variables (if you just need to update one variable, you can get the function to return the value and assign it to the required variable).

The most popular example is the swap function:

function swap(float &a, float &b)
  float tmp;
  tmp = a;
  a = b;
  b = tmp;
endfunction


x = 1; y = 2;
echo("x="+x+" y="+y);
swap(x,y);
echo("x="+x+" y="+y);       

When Gsharp compiles a function it has to know whether to compile the parameters as being passed by value or passed by reference. If it doesn't know then it assumes that they are passed by value.

Gsharp can only find out how the parameters should be passed by compiling the function. So if you want to pass parameters by reference you must make sure that your function definition comes before it used. If you ran the following code in a new Gsharp (i.e. with no knowledge of the swap function) then the Gsharp would assume the parameters should be passed by value and the code would not work.

x = 1; y = 2;
echo("x="+x+" y="+y);
swap(x,y);
echo("x="+x+" y="+y);   

function swap(float &a, float &b)
  float tmp;
  tmp = a;
  a = b;
  b = tmp;
endfunction

Always make sure that functions that pass parameters by reference appear before they are used.

Special care should be taken when working with multiple files. For example, assume that our swap function is in a library called libswap.gsl. You should add this library to your code with the include statement

include "libswap.gsl";

but you might have been tempted to use the exec command:

exec("libswap.gsl");

When Gsharp runs a script it first compiles the entire script and then executes the entire script. You can use the include command to include another script during the compile stage. This will mean that the contents of libswap.gsl are compiled when the include line is compiled and so the compilation of swap() appears before the compilation of the call to swap(). If you use exec, then Gsharp just compiles the exec command, not the file itself. The contents of libswap.gsl are not compiled until the execution of the script, which means the compilation of swap() will occur after the compilation of the call to swap(). Gsharp assumes the parameter were passed by value and the code will not work.

Where possible, use include filename rather than exec(filename)