Functions with Parameters

Hello! I’m having some difficulty using parameters inside of a function in VEXcode Pro V5 Text. My intention is to optimize my program so the code that is rewritten multiple times can be called in just a single function with a few parameters to allow for some variability. My code is below with the error I’m coming upon visible in a popup.

Welcome to the forum @2664C1 !

Thank you for posting your code! In the future, it would be better if you copied and pasted it in with some [code] ... [/code] tags.

So it appears that you are trying to pass a direction into your driving function. Some knowledge of C++ should help you out here.

directionType is actually an enum. What you want to do here is pass an enum to a function. Using a char is not the correct way to do this.

If you do some research (using a search engine of your choice perhaps?), you can probably find a solution.

Feel free to ask more questions!


Thanks for the reply, in the future I’ll remember to use code tags to format it better. While I’m not new to programming C++ isn’t my forte, so I’ve been relying on translating from other languages and fixing syntax errors I come across, but sometimes I end up with a situation like this I can’t solve on my own, I’ll look into enums now, and thanks again.

1 Like

This is correct but I think we can provide a little more insight. Your idea to simplify repeatable steps is commendable and any programmer that finds themselves repeating code should take advantage of functions. The DRY programming principle is an acronym for Dont Repeat Yourself.

In your code, you need to pass a direction to your method and c++ is very type specific. If you specify directionType as a property of the function then that value can then be passed along to the spin function.

void driving(directionType l, directionType r){
  leftMotor.spin(l, 75, pct);
  rightMotor.spin(r, 75, pct);

To take it one step further, I am curious why you wouldn’t pass a value for speed instead of direction.

void driving(double leftSpeed, double rightSpeed){
  leftMotor.spin(fwd, leftSpeed, pct);
  rightMotor.spin(fwd, rightSpeed, pct);

Now you can pass a range of values (-100 to 100) to the chassis. You might even add some more code to ignore values under 25 (deadband).

Here is the bonus: If the rest of your code only calls the driving function to operate the chassis and you decide to change your motor setup, you only need to update the code in one spot. Eg: Switch from a two motor chassis to a four motors and you only have to update your driving function. Everything else will just work.

Good luck!


Hey, thanks for the extra insight! The reason I’m not passing a speed value is because at the moment I only need this to be able to change whether or not the bot is moving forward, backward, or turning for an “Xbox” style control scheme one of my Bot Pilots wants, but I’ll definitely talk with him about speed variations and put the knowledge you’ve offered to good use.

1 Like