NL Functions
NL Functions
Reference
Carnegie Mellon Robotics Academy / For use with VEX Robotics Systems
Functions 1
ROBOTC
Reference
Advanced Functions
Parameters Parameters are a way of passing information into a function, allowing the function to run its commands differently, depending on the values it is given. It may help to think of the parameters as placeholders all parameters must be filled in with real values when the function is called, so in the places where a parameter appears, it will simply be replaced by its given value.
1. Declare parameter A parameter is declared in the same way that a variable is (type then name) inside the parentheses following the function name. 2. Use parameter The parameter value behaves like a placeholder. Whatever value is provided for the parameter when the function is called will appear here. 3. Call function with parameter When the function is called, a value must be provided within the parentheses to take the place of the parameter inside the function.
Substitution The arrows in the illustration to the right show the general path of the value from the place where it is provided in the function call, to where its value is substituted into the function. The function will run as if the code read as it does in the bottom box.
Carnegie Mellon Robotics Academy / For use with VEX Robotics Systems
Functions 2
ROBOTC
Reference
Advanced Functions
Return Values Not all functions are declared void. Sometimes, you may wish to capture a mathematical computation in a function, for instance, or perform some other task that requires you to get information back out of the function at the end. The function will return a value, causing it to behave as if the function call itself were a value in the line that called it.
1. Declare return type Because the function will give a value back at the end, it must be declared with a type other than void, indicating what type of value it will give. 2. Return value The function must return a value. In this case, it is returning the result of a computation, the square of the parameter. 3. Function call becomes a value The function call itself becomes a value to the part of the program that calls it. Here, it is acting as an integer value for the wait command.
int { }
Substitution The arrows in the illustration to the right show the general path of the value as it is returned and goes back into the main function. The parameter 100 is used (as t in the function) to calculate the value, but is not shown in the arrows. The function will run as if the code read as it does in the bottom box.
int { }
wait(10000);
Carnegie Mellon Robotics Academy / For use with VEX Robotics Systems
Functions 3