View unanswered posts | View active topics It is currently Sun Dec 17, 2017 4:15 am






Reply to topic  [ 3 posts ] 
Calling functions in RobotC for VEX Cortex 
Author Message
Rookie

Joined: Wed Oct 16, 2013 7:31 pm
Posts: 2
Post 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.


Fri Mar 04, 2016 9:08 pm
Profile
Expert

Joined: Thu Dec 01, 2011 12:07 am
Posts: 151
Post 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
}


Sun Mar 06, 2016 8:39 pm
Profile
Moderator
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 207
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Post 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

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot


Tue Mar 08, 2016 4:53 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.