Wanting to set default values for a function

Hi everyone. I'm trying to make a function that will sweep a servo back and forth. I am going to need to add things like step size and the delay between steps. These wont be needed very often in my code. However there will be particular times when I would like to have control of the step size and time between steps.

It would be a pain in the neck if I had to write out each and every time what values I would like them to be. I would rather that if they were undefined then they would default to a value. for example if I had a code with the following inputs (obviously with the function after it)

void Scan(int ServoStep, int upperLim, int lowerLim, int delayTime)

Then I would like to be able to define just the first 1, 1-2, or 1-3 terms with the others defaulting to a pre set value.

I tried the following, but just got Scan was not declared in this scope when I tried to call it.

void Scan(int ServoStep = 1, int upperLim = 124, int lowerLim = 45, int delayTime = 10)

Is there any way of doing this in arduino? I'm more used to python, so I was expecting this to work.

Thanks, David.

void Scan(int ServoStep = 1, int upperLim = 124, int lowerLim = 45, int delayTime = 10) { }

void setup() { }

void loop() { }

Compiles without error.

Ah, ok thanks. I think Ive fixed it. The function needed to be declared before the call to it. :stuck_out_tongue: . Thanks again!

The function needed to be declared before the call to it.

Normally, the IDE handles that for you, but, of course, it doesn't understand default values for arguments. So, if you want the function to have default arguments, you either provide the function prototype or you define the function before you reference it.