Programming instructions

Reference
Project Lead The Way
©
and Carnegie Mellon Robotics Academy
©
/ For use with VEX
®
Robotics Systems
Functions 1
void rotateArm()
{
startMotor(armMotor,63);
wait(3.25);
stopMotor(armMotor);
}
task main()
{
rotateArm();
}
A function is a group of statements that are run as a single unit when the function is called from
another location, such as task main(). Commonly, each function will represent a specic behavior
in the program.
Functions offer a number of distinct advantages over basic step-by-step coding.
They save time and space by allowing common behaviors to be written as functions, and then
run together as a single statement (rather than re-typing all the individual commands).
Separating behaviors into different functions allows your code to follow your planning more
easily (one function per behavior or even sub-behavior).
Through the use of parameters, multiple related (but not identical) tasks can be handled with
a single, intuitive function.
1. Declare Your Function
Declare the function by using the word “void”,
followed by the name you wish to give to the
function. It’s helpful to give the function a
name that reects the behavior it will perform.
Within the function’s {curly braces}, write the
commands exactly as you would normally.
When the function is called, it will run the lines
between its braces in order, just like task main
does with the code between its own braces.
2. Call Your Function
Once your declare your function, it acts like a
new command in the language of ROBOTC.
To run the function, simply “call” it by name
– remember that its name includes the
parentheses – followed by a semicolon.
Using Functions
Functions must be created and then run separately. A function is created by “declaring” it,
and run by “calling” it.
Functions with Natural Language
Go to Reference Links