Function call with arguments

Can't pass arguments to function.

    void renderFF(void (*draw_fn)((*t1), (*t2), (*t3)) )
    {
      uint32_t time;
      time = millis() + 10;

      do {
        u8g2.clearBuffer();
        draw_fn(t1, t2, t3);
        u8g2.sendBuffer();
      } while ( millis() < time );
    }
gfx.renderFF(message.info("Hello!", "I am console,", "how are you?"));

What are the types of t1,t2 and T3?

type String

How would the compiler know that?

    void renderFF( void (*draw_fn)(String *a, String *b, String *c) )
    {
      uint32_t time;
      time = millis() + 10;

      do {
        u8g2.clearBuffer();
        draw_fn(a, b, c);
        u8g2.sendBuffer();
      } while ( millis() < time );
    }
1 Like

To pass both a function pointer and its arguments, you can use something like this:

void renderFF(
    void (*draw_fn)(String, String, String),
    String a, String b, String c) {
  // ...
  draw_fn(a, b, c);
  // ...
}

Then you need a function that is of the right type, e.g.,

void f(String a, String b, String c) {
  // Do something with a, b and c.
}

And you can call this as follows.

renderFF(f, "brave", "new", "world");

Note that this will not work for class methods (like message.info), that requires a more complicated setup.

1 Like

that's the syntax to call a function.
Parameters should stand separated as proposed by @jfjlaros
with a bit of work you could use the ellipsis ... to use variable number of parameters.

something like this:

void printText(const char * text) {
  Serial.println(text);
}

void renderFF(void (*draw_fn)(const char *), int count, ...)
{
  va_list list;	                      // va_list from <cstdarg>. list is its type, used to iterate on ellipsis
  va_start(list, count);              // Initialize position of va_list
  for (int i = 0; i < count; i++) { 	// Iterate through every argument
    draw_fn(va_arg(list, const char *));
  }
  va_end(list);	// Ends the use of va_list
}

void setup() {
  Serial.begin(115200);
  renderFF(printText, 3, "Hello!", "I am console,", "how are you?");
}

void loop() {}

1 Like

Thank you so much! I completely forgot that you can do this. I thought and puzzled over the pointers. The solution is simple. Thanks again!

Thank you so much! An interesting decision.

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