GPC Developer Guides

User Created Functions

As well as having a significant number of built-in functions, GPC allows a user to create their own custom functions. A function can run any code valid in the main section and code is also executed in the order it is written.

GPC


Calling a Function

GPC


To call (or run) a function, you simply type its name and put any parameters it requires in between ( and ). When a function is called, the code within it is executed and the return value is sent back to where it was called from. User functions are what is known as global scope, this means they can be called from the init, main and combo sections. They can even be called from within another function, however, GPC does not support recursive calls of functions. This means a function cannot be called from within itself.

Function Name and Declaration

GPC


To declare a function type function followed by a name and (). Within the parenthesis () you place the names of any parameters you would like the function to have if any. Function names and parameters follow the same rules as a variable, they can start with either an underscore ( _ ) or a letter and can be followed by any combination of letters, digits or underscores.

🔴 Syntax

function <name> ( <Parameter(s) >);

Parameters

<name>: The name of the function <Parameter(s) > : Optional parameters. You can use as many as you wish or none at all. Each one must be separated with a comma (,)

Function Parameters

GPC


Function parameters can be thought of as local variables as they cannot be accessed outside of the function they are defined within. A value can be passed to them and they can be used within the function just like a variable could.

As GPC only supports one data type (16bit Integers) you do not need to specify the data type of parameters within a function and the name of a parameter follows the same rules as a function or variable, they can start with either an underscore ( _ ) or a letter and can be followed by any combination of letters, digits or underscores. Function parameters are optional. You are not required to have any at all. The example above is perfectly valid.

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 it 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 the desired value. In the following example, you will see a couple of uses for the return command;

GPC


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.

GPC


The code in myfunction() is then run. If XB1_VIEW is not being pressed, the code nested in the statement:

GPC


XB1_VIEW is ignored as the if statement is FALSE. So the next line executed in the function is:

GPC


at which point the value of 1 is returned to the statement;

GPC


Thus making the above statement TRUE and the code;

GPC


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;

GPC


As you can see, if XB1_A or XB1_B is 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:

GPC


at which point a value of 0 is returned to the statement;

GPC


making it FALSE, so the code;

GPC


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;

GPC


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 let's 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 let's take a look at the declaration of the function 'adjust_speed';

GPC


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;

GPC


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 fourth parameter (10 in this case) is added to the value of var. The value of var is 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 fourth parameter is subtracted from the value of var. The value of var is 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.