0

I'm trying to do something to this affect:

const string first_color = strip.Color(0, 60, 0, 0);

With the intended outcome being that I could use first_color wherever I'd have used strip.Color(0, 60, 0, 0) before.

  • Are you asking how to save the return value of the function so that you can reuse it, or are you asking how to create a reference to the function so that you can call it by invoking first_color? There doesn't seem to be much point to calling it repeatedly with the same parameters, unless it has some side-effects that you're interested in. – Dave Tweed May 12 '16 at 23:42
  • It sounds like you might want a preprocessor macro. – Chris Stratton May 13 '16 at 16:19
1

You pretty much need a helper function to do that; a small local function that calls strip.Color() with the intended arguments and returns strip.Color()'s return value if there is any.

int first_color(void)
{
   return(strip.Color(0, 60, 0, 0));
}

(assuming strip.Color() returns an int) which you would call with:

value = first_color();

If strip.Color() is a void function, then delete the 'return(' and one ')', fix the definition of first color to be void instead of int, and of course, don't try to use any return value from first_color().

0

It sounds like you might want a preprocessor macro:

#define FIRST_COLOR strip.Color(0, 60, 0, 0)

And then later

FIRST_COLOR;

This is basically just text substitution at the initial stage of the compilation process. It is somewhat conventional to make macros all caps, but not required. You can also define function-like macros which take arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.