How to use the button pressed function with parameters

I already know how to use the controller.buttonA.pressed(function); command, but how do you use it like this: controller.buttonA.pressed(function(parameter1, parameter2));

Here is a snippet of my code that uses those functions and the errors I got:
Controller1.ButtonA.pressed(driveDistance(10, 100));

[clang] Cannot initialize a parameter of type ‘void (*)()’ with an rvalue of type ‘void’

Can anybody tell me why this error has occured and how I can fix it?

P.S. I know that this has to be possible, I just have not figured it out…

It’s not possible, events like ButtonA.pressed cannot accept parameters.

7 Likes

Then that is something VEX needs to add. I don’t see any reason why it shouldn’t be possible. I am pretty amazed that I actually stumbled upon something that is not possible in programming. I do not see that often. I know someone that used to program in C++ for his job and every time I have talked with him about a problem that I can’t solve he always says that there is a way. For this apparently not.

You could assign the values you want to pass to the function to globals, set them prior to calling the function, then have the function read the globals when called.

Its not that it is impossible, just impossible as you wrote it.

5 Likes

OK. What I was trying to do was make it move 10 inches every time I pressed the button. Could this also work?
if (Controller1.ButtonA.pressing()) {
driveDistance(10, 100);
}

If all you want to do is call driveDistance(10, 100); every time the button is pressed then you would do this.

void drive_function() {
  driveDistance(10, 100);
}

and then somewhere register it like

Controller1.ButtonA.pressed( drive_function );

The question you asked was completely different and what you wrote wasn’t what the question even asked.

Controller1.ButtonA.pressed(driveDistance(10, 100));

This code actually means call driveDistance with parameters 10 and 100 and then pass the result of that function to the pressed function.

probably what you wanted was really

Controller1.ButtonA.pressed( driveDistance , 10, 100 );

where you were passing several parameters into the pressed event registration call, that’s not possible. If you want to write your own event handling code then it would be, but we chose not to do that.

8 Likes

Why?

There are other ways for you to accomplish this without asking for lots of bells and whistles that satisfy your need - but as written the .pressed is quite powerful.

Thank you all. But while this thread is still active, do you know what a memory permission error is?

You have a bad mistake in your code, something like using a null pointer.

9 Likes

Do you need a space around the function name?

No, whitespace is ignored by the C compiler, but I prefer to use more than most people as I feel it makes code more readable.

7 Likes

Oh, ok. Does that mean I can put 100 lines of code on the same line, if there is a semicolon between each?

Yes, but please don’t other than for a laugh - this is a terrible idea from a readability standpoint.

6 Likes

I reading this and I sort of understand it. (Yes I understand this is an old thread) in the pressed(); does it read and execute what’s in the parentheses?

1 Like

image