Declaring function inside the function argument parenthesis

Hello everyone, my question is very simple but as i am not a programming expert i couldn't find the correct solution. I have this code and declaring a function and using its reference as an argument, and every class object has different functions.

class MyClass{
  public:
    uint64_t irCode;
    char btCode;
    void (*fun)(void);
    void init(uint64_t ir,char bt, void(*f)(void)){
        irCode = ir;
        btCode = bt;
        fun = f;
    }
}myclass[2];

void f1();
void f2();

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  myclass[0].init(0x000016,'a',&f1);
  myclass[1].init(0x000018,'b',&f2);
}


void loop() {
  myclass[0].fun();
  myclass[1].fun();
}
  

void f1(){
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(250);                       
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(250); 
}
void f2(){
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(500); 
}

is it possible to declare function inside the init function argument like

myclass[0].init(0x000016,'a',void f1{
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(250);                     
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(250); 
  }
);

i already checked online and c++ 11 supports it but couldn't find the arduino example.

thanks

I think that what you are looking fore are lambda declarations:

pass_a_function([](void) {

  //the code for the lambda goes here

});

This syntax has some varieties, so search for a better understanding.

P.S: Sorry, but the new forum has a weird way of (not) formatting BB-code, so it got screwed.

1 Like

You can see how this is written / done in this example (In the ESPAsyncWebServer library)

server is the instance of the AsyncWebServer class and On() is the method that takes a function pointer as argument and here a lambda is used

1 Like

kernighan explains pointers to functions in The C Programming Language (pdf pg 87)

thank you all guys, lambda declaration saves me. for future visitors i am leaving the final code, though the functions do the same thing, it explains the idea.

class MyClass{
  public:
    uint64_t irCode;
    char btCode;
    void (*fun)(int);
    void init(uint64_t ir,char bt, void(*f)(int)){
        irCode = ir;
        btCode = bt;
        fun = f;
    }
}myclass[2];


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  myclass[0].init(0x000016,'a',[](int a){
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(a);                       
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(a);  
    }
  );
  myclass[1].init(0x000032,'b',[](int b){
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(b);                       
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(b);  
    }
  );
}


void loop() {
  myclass[0].fun(250);
  myclass[1].fun(750);
}

Defining a type for your function pointer would tighten up the code a little:

class MyClass{
  using FunctionPtr = void (*)(int);
  public:
    uint64_t irCode;
    char btCode;
    FunctionPtr fun;
    void init(uint64_t ir,char bt, FunctionPtr f){
        irCode = ir;
        btCode = bt;
        fun = f;
    }
}myclass[2];


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  myclass[0].init(0x000016,'a',[](int a){
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(a);                       
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(a);  
    }
  );
  myclass[1].init(0x000032,'b',[](int b){
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(b);                       
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(b);  
    }
  );
}


void loop() {
  myclass[0].fun(250);
  myclass[1].fun(750);
}
1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.