### Call by Value

Like C/C++, arguments to SPL functions are passed by value. Functions receive private and temporary copies of the argument variables. This means that a function cannot alter the original argument in the calling function. Within a function, each argument is in effect a local variable initialized to the value with which the function was called. Unlike C/C++, call by value also holds for series and array arguments.

The only exception to the call by value rule occurs when series and array arguments are manipulated directly within an SPL function via the explicit square bracket [] array references. In this case, the original series and array arguments are altered by the SPL function. For example:

/* call by value rule */

double1(x)

{

x *= 2;

return(x);

}

/* exception to call by value */

double2(x)

{

local n, j;

n = length(x);

loop (j = 1..n)

{

x[j] *= 2;

}

return(x);

}

Both functions double each element of a series, however double1 does not alter the original input series whereas double2 does alter the series. Note: double1 is also faster and can be applied to scalars but double2 only operates on arrays. For example:

a = {1, 2, 3}

double1(a) returns the series: {2, 4, 6}

Series a still contains the original values {1, 2, 3}.

double2(a) also returns the series: {2, 4, 6}

but series a now contains the values {2, 4, 6}.

To avoid this call by value exception when using array references, simply assign the input series to a local variable.

/* does not alter input series */

double3(x)

{

local local_x, n;

local_x = x;

n = length(x);

loop (j = 1..n)

{

local_x[j] *= 2;

}

return(local_x);

}