Returning from a function
return is a command unique to functions. It is not mandatory for each user function to have a return value though. If there is no return in a function, then 0 (zero) will be automatically returned.
You can have multiple return points within a function. Once the first return command is executed, the function returns a value to where is was called and the function is terminated. The code beyond that point in the function will not be run.
Returning a value is one of the single most useful commands within a function as it can be used as a boolean value to enable or disable sections of code, to set parameters in other functions or to set a variable to a desired value. In the following example, you will see a couple of uses for the return command;
Putting it all together
When the GPC script is first loaded, the two variables RF_HOLD and RF_NULL are created with a value of 40 and 30 respectively.
The main section then starts its first iteration (run). When it gets to the line below the myfunction() function is executed.
The code in 'myfunction()' is then run. If XB1_VIEW is not being pressed, the code nested in the statement:
is ignored as the if statement is FALSE. So the next line executed in the function is:
at which point the value of 1 is returned to the statement;
Thus making the above statement TRUE and the code;
is executed. The main section carries on until it reaches its end point and loops.
However, if XB1_VIEW is being held when 'myfunction()' is executed, then the following code is run;
As you can see, if XB1_A or XB1_B are not also held down, the code set a few buttons to 0, writes the value of our two variables to TRACE values and then most importantly, reaches the line:
at which point a value of 0 is returned to the statement;
making it FALSE, so the code;
which is nested within that statement is ignored and not executed.
If XB1_VIEW and XB1_A are both held when 'myfunction()' is executed, then the following line of code is reached and run;
what the above line means is the variable RF_HOLD equals the return value of the function 'adjust_speed' or you could say the return value from 'adjust_speed' is stored in RF_HOLD. So lets take a look at how that function returns a value.
As you can see above, four values are being sent to the function 'adjust_speed'. The value of RF_HOLD, 10, 1000 and 10. So lets take a look at the declaration of the function 'adjust_speed';
function 'adjust_speed' requires 4 arguments, the variable to be adjusted, the minimum value you want it to be, the maximum value you wish for it to be and how much to adjust it by each increment.
To manipulate the variable, the function executes the following code;
In the first part of this code, if XB1_RB is pressed and the variable value passed to the function is less than the maximum value allowed, the value passed in the forth parameter (10 in this case) is added to the value of var. The value of var is the returned to where the function is called. Therefore making RF_HOLD equal 10 more than it did before.
If XB1_LB is pressed and the variable value passed to the function is greater than the minimum value allowed, the value passed in the forth parameter is subtracted from the value of var. The value of var is the returned to where the function is called. Therefore making RF_HOLD equal 10 less than it did before.
An identical process is carried out if XB1_VIEW and XB1_B are pressed when 'myfunction()' is executed with the exception being that RF_NULL is adjusted rather than RF_HOLD.