ROBOTC.net forums
http://robotc.net/forums/

Calling functions in RobotC for VEX Cortex
http://robotc.net/forums/viewtopic.php?f=11&t=13556
Page 1 of 1

Author:  bjwhited [ Fri Mar 04, 2016 9:08 pm ]
Post subject:  Calling functions in RobotC for VEX Cortex

I am trying to get my team to start writing programs incorporating functions but am having trouble.

I have started with two simple functions: forward and leftPointTurn, both using time as a parameter in the function definition. The time parameter is then used in the wait1Msec call.

In the 'main' function I have used “startTask” to call the functions.

This is my program so far:
1 // Trying to create functions for movement and turns that can then be called in the 'main' function.
2
3 task forward(time)
4 {
5 motor[rightMotor] = 127; //motor moves forward at full power
6 motor[leftMotor] = 127; //motor moves forward at full power
7 wait1Msec(time);
8 }
9
10 task leftPointTurn(time)
11 {
12 motor[rightMotor] = 127; //motor moves forward at full power
13 motor[leftMotor] = -127; //motor moves backward at full power
14 wait1Msec(time); //wait using the time specified by the parameter in the function call
15
16 }
17 task main()
18 {
19 // your code goes here
20 startTask(forward(2000)); //call the forward function with time parameter of 2000
21 startTask(leftPointTurn(450)); //call the leftPointTurn function with time parameter of 450
22 }

The program does not compile due to numerous errors. The errors are listed below:
3 **Error**:Ummatched left parenthesis '('
3 **Error**:Expected->')'. Found 'time'
3 **Error**:Undefined variable 'time'. 'short' assumed.
3 **Error**:Missing ';' before ')'
3**Error**:Executable statements not valid in 'main' declaration block
3 **Error**:Unexpected ')' during parsing
3 *Warning*:Meaningless statement -- no code generated
5 **Error**:Undefined variable 'rightMotor'. 'short' assumed.
6 **Error**:Undefined variable 'leftMotor'. 'short' assumed.
10 **Error**:Ummatched left parenthesis '('
10 **Error**:Expected->')'. Found 'time'
10 **Error**:Missing ';' before ')'
10 **Error**:Executable statements not valid in 'main' declaration block
10 **Error**:Unexpected ')' during parsing
10 *Warning*:Meaningless statement -- no code generated
11 **Error**:Duplicate definition for task 'main'.
17 **Error**:Duplicate definition for task 'main'.
20 *Warning*:Substituting similar variable 'StartTask' for 'startTask'. Check spelling and letter case.
20 **Error**:Undefined procedure 'forward'.
21 *Warning*:Substituting similar variable 'StartTask' for 'startTask'. Check spelling and letter case.
21 **Error**:Undefined procedure 'leftPointTurn'.

Thanks to anyone that can help me get this running.

Author:  CARBOT [ Sun Mar 06, 2016 8:39 pm ]
Post subject:  Re: Calling functions in RobotC for VEX Cortex

Use 'void' in defining a function. forward may be a reserved word.
Does this get you toward where you want to be?

Code:
// Trying to create functions for movement and turns that can then be called in the 'main' function.

void forward1sec(int time)
{
   motor[rightMotor] = 127;   //motor moves forward at full power   
   motor[leftMotor] = 127;   //motor moves forward at full power   
   wait1Msec(time);   
}

void leftPointTurn(int time)
{
   motor[rightMotor] = 127;   //motor moves forward at full power   
   motor[leftMotor] = -127;   //motor moves backward at full power
   wait1Msec(time);   //wait using the time specified by the parameter in the function call   

}
task main()
{
   // your code goes here
   forward1sec(2000);   //call the forward function with time parameter of 2000
   leftPointTurn(450);   //call the leftPointTurn function with time parameter of 450
}

Author:  Coder A [ Tue Mar 08, 2016 4:53 pm ]
Post subject:  Re: Calling functions in RobotC for VEX Cortex

The issue is that you have defined your 'functions' as tasks by using the task keyword. Tasks are different from functions, which are preceded in definition by the type of data they return. In your case both functions return void, so replace task with void and your problem is solved.

Forward is not a reserved word, so you don't have to bother with changing it.

I'm just offering a longer explanation than CARBOT did, because he already provided you with a working program.

-A

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/