Programming instructions

Reference
Project Lead The Way
©
and Carnegie Mellon Robotics Academy
©
/ For use with VEX
®
Robotics Systems
Functions 3
int squareOf(int t)
{
int sq;
sq = t * t;
return sq;
}
task main()
{
startMotor(rightMotor,63);
wait(squareOf(100));
stopMotor(rightMotor);
}
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.
int squareOf(int t)
{
int sq;
sq = t * t;
return sq;
}
task main()
{
startMotor(rightMotor,63);
wait(squareOf(100));
stopMotor(rightMotor);
}
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.
wait(10000);
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.
Go to Reference Links