Function not working

Hi. I am trying to use a void function, but It is not working for some reason. I want to be able to type something like DriveForward(forward, 10, inches); or something like that. here is what I have.

int main() {
//Below me
void DriveForward() {
Drivetrain.driveFor};
//Above me
// create competition instance
competition Competition;
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(userControl);

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

// Prevent main from exiting with an infinite loop.
while (true) {
wait(100, msec);
}
}

Dont worry about the rest of it. just the function is what I want to work on. It gives me an error that says function not allowed here. thanks!

Several things. First post your code with “```” marks on a line by itself, followed by your code and then another line with just “```”. So your code will look like:

int main() {
//Below me
void DriveForward() {
Drivetrain.driveFor};
//Above me
// create competition instance
competition Competition;
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(userControl);

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

// Prevent main from exiting with an infinite loop.
while (true) {
wait(100, msec);
}
}

Let’s look at your first line: int main() { which is defining, in the “global scope” a new function called main, which returns an integer and takes no arguments. In addition to defining a function, this creates something called a scope. Your next line void DriveForward() { also defines a new function, this time called DriveForward within the scope of the function main. Generally as a beginner, you do not want to define functions inside the scope of another function.

So lets change your program to:

// I may have some of these types wrong, you should be able to figure out what they should be
void DriveForward(vex::directionType dt, double amt, vex::distanceUnit du) {
Drivetrain.driveFor(dt, amt, du); // not sure what this actually takes, but you should get the idea
}

int main() {
DriveForward(vex::directionType::fwd, 10, vex::distanceUnit:in);
// Prevent main from exiting with an infinite loop.
while (true) {
wait(100, msec);
}
}

Because you haven’t “declared” your function DriveForward you’ll want to define it before you use it in the main function.

You have now defined a function called DriveForward which takes 3 arguments - a direction (forward or backward), an amount, and a unit of measure. You are then passing these values into the Drivetrain instance’s driveFor method.

4 Likes

Thank you! What would me trying to use the function in autonomous look like? like
DriveForward(what-goes-here?);

Thank you for helping me!

Make sure that you put your void DriveForward function higher in the file than where you call it. In this case, make sure it is in the file before the function definition for void autonomous()

You should then be able to:

void DriveForward(vex::directionType dt, double amt, vex::distanceUnit du) {
  Drivetrain.driveFor(dt, amt, du); // not sure what this actually takes, but you should get the idea
}

void autonomous() {
   DriveForward(vex::directionType::fwd, 10, vex::distanceUnit::in);;
}

int main() {
  competition Competition;
  Competition.autonomous(autonomous);

  preAutonomous();
  while (true) {
    wait(100, msec);
  }
}
1 Like

Ok. Thank yoy!
20 chae