RobotC Tutorial 3 – Functions

less than 1 minute read

To make life easier when programming, sometimes people may use things called functions. Functions are blocks of code that perform a specific task. Fuctions can be used to reduce the complexity of a code, and to make it more clear. In RobotC, I use functions a lot because they help other people understand what the code does.

Let’s start out basic. Functions in RobotC are made by typing void name(). Replace name with the name of your function, and you’re set. Let’s take a look at a sample program I made.

void one()
{
motor[motorB] = 50;
motor[motorC] = 50;
}
void two()
{
motor[motorB] = -50;
motor[motorC] = -50;
}
task main() {
one();
wait1Msec(1000);
two();
wait1Msec(2000);
}

This is a simple example of functions. In the task main, it runs the function “one”, which codes for going forward, for one second. I then runs “two”, which codes for going backward, for two seconds. In order to include a function defined outside of the main function, you use the format “name();”. Functions can be used for much more than this, but this is a simple example. We will be using them more in the next tutorial, using sensors.

Leave a Comment