Having a function have a default value

I'm not sure what this is called.

I notice that pulseIn takes three arguments. One is the pin, one is the value of the pulse to be measured, and one is the timeout. If no timout is passed to pulseIn, it times out in 1 second be default.

I can think of one way to create a function with this kind of "defaulting" behavior...create two functions, one with three arguments and another with only two arguments that has the third argument hard-wired to a default value. I think that's called function overloading. But that's wasteful.

Is there a better way to do this ? I think in java I think you can do something like this when you create a function

long myfunction(int pin=1, bool me=1, int time=1, long time1=1)

then if you use the function else where, if you just do


then it will be the same as running


and if you ran


it would be the same as running

myfuncition (2,2,1,1);

does this work in arduinoland?

"Arduinoland" is C++. Unless the tab name specifically ends in .c not .cpp or anything else.

So yes, you can do default argument values. At the first declaration, specify the values. Don't specify the values again if there is a second declaration. All arguments with no defaults must come first. All defaults must be expressions that the compiler can compute at compile time, not code that must be executed at runtime.

The only quirk about default values in C++ is that you can only omit values if there are no additional arguments to the right of the value you are omitting. I think some languages have a syntax that allows it.

In other words given

long myfunction(int pin=1, bool me=1, int time=1, long time1=1)

you can say

myfunction( 4, 3, 5 ) - omitting time1


myfunction( 3, 5 ) - omitting time and time1

but there is no way to omit pin and specify the other three

myfunction( , 3, 5, 4 ) - won't work

OK, that makes sense.

By the way, does "bool" work for boolean? I know I typed it here but Arduino doesn't highlight it.

Furthermore, is "HIGH" and "LOW" equivalent to "true" and "false" is equivalent to 1 and 0?