Help in vex code

Hello good morning, I want to ask anyone for help to teach me how to do functions in vex code, since I want to do functions like in robot c and it won’t let me, can someone help me please

In VEXcode Pro V5 or just plain VEXcode V5? The topic for this thread is VEXcode VR.

In general, custom functions typically are first defined by what value you want to return, followed by the function name, and what inputs you want and their data types. For example, I could have the function:

int multiplication(int x, int y)

which would take the integers x and y as input and output an integer. It should be noted that if you don’t want to have an input or an output, you could simply just say void. For example:

void function(void)

To set it up, you would do:

int multiplication(int x, int y){
    int answer = x * y;
    return answer;
}

This would be the definition of the function. This would basically be saying what to do with said inputs. In this case, I am saying to multiply x and y and return that value. When your function is outputting a value, there has to be something that is returned.

Depending on your programming environment, you could do one of two things. If you are using VEXcode V5 in the text setting or VEXcode V5 Pro, you have to put your definition of the function above when you use the function. For example:

int multiplication(int x, int y){
    int answer = x * y;
    return answer;
}

int main(void){
    Brain.Screen.printf(multiplication(5,6));
}

As you can see, I first defined the function, then used it in later in the program.

2 Likes

In vexcode v5 sorry, you don’t know how to make a function in vex code v5 and put a variable with with int to invoke them later in the task as a void forward (int speed, int time) and already in the task put the value desired void forward (100, 1000), that’s how I want to do it in vex code v5 and it doesn’t accept it, it takes it as wrong syntax

You don’t need to put the void in front of the function when using it.

1 Like

And how would the programming be if I want to put sensors

How would be the programming of the function already with sensors

Can you post the current code that you have?

Int turn to the left (int speed, int degrees)

the truth is that I am started but here I no longer understand I can invoke everything that the inertial sensor should do and put where the values ​​of the variable speed and degrees go and then how I invoke in the main task of the competition template that I do not understand sorry

Post you whole code. Also, you would want to call the function during the autonomous task rather than the main task. Finally, you shouldn’t have spaces in your function name.

1 Like

Int turnright (int speed, int degrees)

while (Gyro.rotation (degrees) <degrees) {
LeftMotor.spin (directionType :: fwd, speed, velocityUnits :: pct);
RightMotor.spin (directionType :: fwd, -speed, velocityUnits :: pct);
}

So I have my code that you recommend me to change and how do I invoke it in the friend competition template?

You would just call the function in your auton. For example:

void autonomous(void){
     turnright(89, 78);

Also, your function turn right isn’t returning anything, so you should just change it to

void turnright(int speed, int degrees)|
    //definition of turnright
4 Likes

Or I already understood, that is, in the first one it is to invoke the function in the autonomous and the second is how to make the function above the function could be done in the part of pre auton truth, and below
void turnright (int speed, int degrees) I can put what I want the robot to do true

If what you are trying to say is that the first snip of code would be how you would implement it, then that is true. The second snip of code is your function definition and shouldn’t be located inside of another function. As a result, it shouldn’t be defined during pre-auton. Below the function definition, you can put what you want the robot to do. For example, if I wanted to create a function that multiplied two numbers and outputted the result, I would do something like this:

void multiplication(int x, int y){
     int answer = x * y;
     Brain.Screen.printf(answer);
}
2 Likes

So this is what the friend program would look like?

Pre auton () {

Void turnleft (int speed, int degrees) {

What I want the robot to do

}
}

Void autonomous (void) {

turnleft (100, 90)

}

Don’t put the function definition in the preauton. What would happen is that the function would only exist during the preauton function. Also, I don’t even think it will allow you to define a function inside of a function definition.

void turnLeft(int speed, int degrees){
//what robot does when turning left
}

Pre-auton(){
}

Void autonomous(void){
turnLeft(100, 90);
}

Also, I should note that you should use camelcase when naming multiword variables and functions. Camelcase is basically when the first letter of the first word isn’t capitalized and the first letter of the every next word is capitalized. For example: motorVelocity.
Alternatively, you could just use lowercase for all letters and split up the words with an underscore. For example: motor_velocity.

The reason why these naming conventions exist is for readability of the program.

5 Likes

Ooo I understand the calmecase, what I understand is that the first written word must be lowercase and the next in uppercase and alternate and so on.

And finally, in the part of the program, I do not have to declare the function and the variable ever in the pre-auton part because it would only be executed there, right, and in the other part of the program, it’s okay, right, friend?

Yes. That would be correct.

To clarify for the camelcase, if I have three words, it would be like this: leftMotorVelocity.

2 Likes

Thank you very much friend for clarifying this for me, you should be a teacher.

1 Like