Function overloading and other ways for variable number of arguments

something I’ve had a hard time understanding is how the random() function works with different options of inputs.
For example:

random (max);
random (min, max);

how is it that putting a second input automatically designates the original reference to that instead of the first position?
the reason why I ask is because I’m trying to learn how to make my own functions where if I don’t put all the variables in it can still work intelligently.
I’ve looked up the random() function on the arudino reference page, but I can’t find anything explaining the inner workings of it. If I look up c++ references it usually advises about sticking to the original set of inputs, but there isn’t any tips on handling absent variables.

UPDATE 1hr later:
i think i found out what this is: overloading functions. im reading up on it now…

The magic word you’re looking for is ‘function overloading’.

You provide several different ‘function overloads’ and the compiler picks the correct one based on the number of arguments and the types of arguments.

Since it was random that sparked the question, take a peek at random's implementation.

long random(long howbig)
{
  if (howbig == 0) {
    return 0;
  }
  return random() % howbig;
}

long random(long howsmall, long howbig)
{
  if (howsmall >= howbig) {
    return howsmall;
  }
  long diff = howbig - howsmall;
  return random(diff) + howsmall;
}

There are two different overloads (actually three, but ignore the no-argument version for now), each with a different definition.

If you write random(5), the compiler picks the long random(long howbig) overload,
and if you write random(2, 8), the compiler picks the long random(long howsmall, long howbig) overload.


Overloading works based on type as well.
For example if I defined:

int test(int a)
{
  return 1;
}

int test(bool b)
{
  return 2;
}

Then calling test(0) would give the value 1,
but calling test(true) would give the value 2.

2 Likes

absolute gold. thank you!

1 Like

Just for completeness (but not used by random()), in addition to function overloading, it’s also possible to designate optional argument(s) that will use a default value if the argument(s) isn’t included in the call to the function.

Optional arguments must be placed at the end of the arguments given in the function prototype. They are specified by putting an equals sign followed by the default value, after the name of the argument.

For example:

void drawObject(int x, int y, unsigned char color = WHITE);

Parameters x and y must be provided but if you leave out color it will be given a value of WHITE.

Like so:

drawObject(5, 10);        // draw object at location 5, 10 with (default) color WHITE
drawObject(6, 20, WHITE); // draw object at location 6, 20 with color WHITE
drawObject(7, 30, BLACK); // draw object at location 7, 30 with color BLACK

There’s also C style “variable number of arguments” available for functions. Plus, C++ specific ways to do variadic functions. I’ll leave the investigation of this up to the reader.

2 Likes

wow 2 beefy answers under 2 hours. this forum is like Christmas. i wish there was some way of showing my support.
i will do my due diligence to release an amazing (brain) game!

will delete this sometime in the future because #unrelated wuz moved!~tanks!

2 Likes

Rather than deleting it, it would be better if it were moved into its own thread.

As far as I’m aware, only moderators can do that though.


More technically, ‘variadic functions’.

1 Like