451 views

# Why am I getting an error for my Drive CM Function???

1. 2 months ago

### abennett5139

Sep 12 Concord, NC 5139

So I'm attempting to make a function for my program to drive a certain number of centimeters using the rotateFor command and I have everything defined but im getting an error that says
15:48:43 -- error -- error: function definition is not allowed here
void DriveforCm (double DistanceCM, char y) {
^

Why am I getting this error to make the function? Any help would be appreciated

Here's my code
```void DriveforCm (double DistanceCM, int veloc) { //Define variables double wheelDiameterCM = 10.16; double travelTargetCM = DistanceCM; //Wil determine how far the robot goes //Calc wheel circumferance double circumference = wheelDiameterCM * M_PI; double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program. //Set the velocity of the left and right motor LeftDrive.setVelocity(veloc, vex::velocityUnits::pct); RightDrive.setVelocity(veloc, vex::velocityUnits::pct); //Rotate the Left and Right Motor for degreesToRotate. LeftDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); RightDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //Stop motors after reached CM distance LeftDrive.stop RightDrive.stop } ```

### Answered by eggplant

See post in context

You have `void DriveforCM` inside `void pre_auton`, it needs to be outside of any functions

so something like this:

```void DriveforCM (double DistanceCM, int y) {

//Define variables
double wheelDiameterCM  = 10.16;
double travelTargetCM = DistanceCM; //Wil determine how far the robot goes

//Calc wheel circumferance
double circumference = wheelDiameterCM * M_PI;
double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program.

//Set the velocity of the left and right motor to 50% power. This command will not make the motor spin.
LeftDrive.setVelocity(y, vex::velocityUnits::pct);
RightDrive.setVelocity(y, vex::velocityUnits::pct);

//Rotate the Left and Right Motor for degreesToRotate.
LeftDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command must be non blocking.
RightDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command is blocking so the program will wait here until the right motor is done.

//Stop motors after reached CM distance
LeftDrive.stop();
RightDrive.stop();
}

void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...

DriveforCM(50, 100);
}```
2. ### eggplant

Sep 12 Wisconsin 9144D
Edited 2 months ago by eggplant

Are you declaring it inside main or another function?

3. ### abennett5139

Sep 12 Concord, NC 5139

@eggplant Are you declaring it inside main or another function?

Yes I'm declaring in `int Main()`

4. ### eggplant

Sep 12 Wisconsin 9144D
Edited 2 months ago by eggplant

@abennett5139 Yes I'm declaring in `int Main()`

You can't have have function definitions inside other functions

So moving it out of main should fix it

5. ### abennett5139

Sep 12 Concord, NC 5139

@eggplant You can't have have function definitions inside other functions

So moving it out of main should fix it

I did that and it didn't work

6. ### eggplant

Sep 12 Wisconsin 9144D

can you post all of the code please

7. ### abennett5139

Sep 12 Concord, NC 5139

@eggplant can you post all of the code please

```vex::competition;

void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
void DriveforCM (double DistanceCM, int y) {

//Define variables
double wheelDiameterCM  = 10.16;
double travelTargetCM = DistanceCM; //Wil determine how far the robot goes

//Calc wheel circumferance
double circumference = wheelDiameterCM * M_PI;
double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program.

//Set the velocity of the left and right motor to 50% power. This command will not make the motor spin.
LeftDrive.setVelocity(y, vex::velocityUnits::pct);
RightDrive.setVelocity(y, vex::velocityUnits::pct);

//Rotate the Left and Right Motor for degreesToRotate.
LeftDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command must be non blocking.
RightDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command is blocking so the program will wait here until the right motor is done.

//Stop motors after reached CM distance
LeftDrive.stop
RightDrive.stop
}

}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              Autonomous Task                              */
/*                                                                           */
/*  This task is used to control your robot during the autonomous phase of   */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void autonomous( void ) {
// ..........................................................................
// Insert autonomous user code here.
// ........................................................................

}

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*                              User Control Task                             */
/*                                                                            */
/*  This task is used to control your robot during the user control phase of  */
/*  a VEX Competition.                                                        */
/*                                                                            */
/*  You must modify the code to add your own robot specific commands here.    */
/*----------------------------------------------------------------------------*/

void usercontrol( void ) {
// User control code here, inside the loop
while (1)
{
vex::task::sleep(20); //Sleep the task for a short amount of time to prevent wasted resources.
}
}

//
// Main will set up the competition functions and callbacks.
//
int main() {

//Run the pre-autonomous function.
pre_auton();

//Set up callbacks for autonomous and driver control periods.
Competition.autonomous( autonomous );
Competition.drivercontrol( usercontrol );

//Prevent main from exiting with an infinite loop.
while(1) {
vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
}

}```
8. ### eggplant

Sep 12 Answer Wisconsin 9144D

You have `void DriveforCM` inside `void pre_auton`, it needs to be outside of any functions

so something like this:

```void DriveforCM (double DistanceCM, int y) {

//Define variables
double wheelDiameterCM  = 10.16;
double travelTargetCM = DistanceCM; //Wil determine how far the robot goes

//Calc wheel circumferance
double circumference = wheelDiameterCM * M_PI;
double degreesToRotate = (360 * travelTargetCM) / circumference; //All calculations are complete. Start the rest of the program.

//Set the velocity of the left and right motor to 50% power. This command will not make the motor spin.
LeftDrive.setVelocity(y, vex::velocityUnits::pct);
RightDrive.setVelocity(y, vex::velocityUnits::pct);

//Rotate the Left and Right Motor for degreesToRotate.
LeftDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command must be non blocking.
RightDrive.rotateFor(degreesToRotate, vex::rotationUnits::deg); //This command is blocking so the program will wait here until the right motor is done.

//Stop motors after reached CM distance
LeftDrive.stop();
RightDrive.stop();
}

void pre_auton( void ) {
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...

DriveforCM(50, 100);
}```
9. ### abennett5139

Sep 12 Concord, NC 5139

@eggplant You have `void DriveforCM` inside `void pre_auton`, it needs to be outside of any functions

so something like this:

Oh so can a void not be inside of a void?

10. ### eggplant

Sep 12 Wisconsin 9144D

@abennett5139 Oh so can a void not be inside of a void?

A function of any type cannot have its definition inside any other function

you can still use functions inside other functions, you just cant define in them

11. ### abennett5139

Sep 12 Concord, NC 5139

@eggplant A function of any type cannot have its definition inside any other function

you can still use functions inside other functions, you just cant define in them

Ok thank you

12. ### eggplant

Sep 12 Wisconsin 9144D

np, if you have any other questions feel free to ask

13. ### John TYler

Sep 12 WA

@abennett5139 "void" and "int" in function declarations and definitions just refer to the data type of the value they return. (You can have ones that return double, *char, int[], etc.) They are distinct from what the thing being defined is. Saying `int main () {}` is defining a function with a return value of type int, and your `void DriveforCM (double DistanceCM, int y) {}` is defining a function with a return value of type void (that is, it returns nothing).

14. ### abennett5139

Sep 13 Concord, NC 5139

@John TYler @abennett5139 "void" and "int" in function declarations and definitions just refer to the data type of the value they return. (You can have ones that return double, *char, int[], etc.) They are distinct from what the thing being defined is. Saying `int main () {}` is defining a function with a return value of type int, and your `void DriveforCM (double DistanceCM, int y) {}` is defining a function with a return value of type void (that is, it returns nothing).

Just a question because I know what integers, chars, floats, etc are but what is a double

15. ### meng

Sep 13 Singapore 8059

@abennett5139 Just a question because I know what integers, chars, floats, etc are but what is a double

double is basically 2 x the precision of a float.
eg. if a float variable is using 32 bits, then a double will be using 64 bits.

16. ### jpearman

Sep 13 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

@abennett5139 Just a question because I know what integers, chars, floats, etc are but what is a double

double precision floating point number.
https://en.wikipedia.org/wiki/Double-precision_floating-point_format