As I cant post in the Technical Support Q&A forum I thought I would post here in response to your question Liam. Firstly, here is a link to a awesome website with heaps of info about functions and C in general: http://www.tutorialspoint.com/cprogramming/c_functions.htm
So to use parameters, simply create a function and add a data type then name within the brackets of a function declaration. You are creating a local variable that can be changed when calling the function from your program.
e.g.
void goForward(int speed)
You can then define the body of the function and use the parameters within the function.
void goForward(int speed)
{
// Set the Drive Motors to the value of speed
motor[leftMotor] = speed;
motor[rightMotor] = speed;
}
During the program you can then call the function, followed by the parameter:
goForward(67);
The Variable is local to the function, not global, so this will not work:
void goForward(int speed)
{
// Set the Drive Motors to the value of speed
motor[leftMotor] = speed;
motor[rightMotor] = speed;
}
task main()
{
motor[arm] = speed;
}
and will return this error on the line with motor[arm] because the variable speed is not global:
To add multiple parameters to a function, divide them with a comma:
void goForward(int speedR, int speedL)
{
// Set the Drive Motors to the value of speed
motor[leftMotor] = speedL;
motor[rightMotor] = speedR;
}
Parameters can also be used on a function with a return type, for example:
float timesByTen(float numb)
{
return( numb*10 );
}
Parameters can also have another variable as input, just define a function with a parameter as you normally would, then during the program input a variable:
int MyNumber = 100;
timesByTen(MyNumber);
By Default C uses ‘Call by value’ if you input another variable, which just means that you do not actually modify the variable you input, rather you make the value of the variable in the parameter the same as the variable you input. In the example above you are essentially just making the value of the ‘float numb’ defined in the parameters of your function the value of ‘MyNumber’. Essentially the same as doing:
numb = MyNumber;
within the parameters.
Parameters can have a default variable value, e.g.
void goForward(int speed = 127)
{
// Set the Drive Motors to the value of speed
motor[leftMotor] = speed;
motor[rightMotor] = speed;
}
Then in code you could call:
goForward();
and because the default value of speed is 127, the motors will go at 127 speed.
A Cool Tip:
Another useful use of functions is to set the value of variable
For Instance:
int Number1 = timesByTen(11);
and because timesByTen outputs the value * 10 Number1 will equal the 110
Really here, we are just scratching the surface of the capabilities of C, but this should get you started.
Good Luck