Skip to content

Instantly share code, notes, and snippets.

@ClemRz
Last active November 19, 2023 16:11
Show Gist options
  • Save ClemRz/1e6c3acaef3c3b8a41dd3b5aeefb0b11 to your computer and use it in GitHub Desktop.
Save ClemRz/1e6c3acaef3c3b8a41dd3b5aeefb0b11 to your computer and use it in GitHub Desktop.
How to pass a callback function as an argument (Arduino, C++)

How to pass a callback function as an argument (Arduino, C++)

Arduino Uno:

bool myCallback(int value) {
  // here your custom logic
  return status;
}

void myFunction(bool (*callback)(int)) {
  // here your custom logic
  bool status = callback(3);
}

void loop(void) {
  myFunction(&myCallback);
}

Using of the std C++ library:

bool myCallback(int value) {
  // here your custom logic
  return status;
}

void myFunction(std::function<bool (int)> callback) {
  // here your custom logic
  bool status = callback(3);
}

void loop(void) {
  myFunction(myCallback);
}
@H0MERS
Copy link

H0MERS commented Jul 31, 2023

Fantastic. this saves my time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment