return from function help

Hi everyone, Im very new to arduino and have a function question, I took a C programming class a couple years ago and never understood this (some how i passed).

here is the code im working with (its a debounce on/off button) Im trying to understand.

const int led=9;
const int button=2;
boolean lastbutton = LOW;
boolean currentbutton = LOW;
boolean ledon = false;

void setup()
{
 pinMode(led, OUTPUT);
 pinMode(button, INPUT);
}
 
 // start debounce
 
 boolean debounce(boolean last)
   {
    boolean current = digitalRead(button);
    if (last != current)
     {
       delay(5);
       current = digitalRead(button);
       
       return current;
     }
   }
   
     
void loop()
{
  currentbutton = debounce(lastbutton);
  if (lastbutton == LOW && currentbutton == HIGH)
  {
    ledon = !ledon;
  }
  lastbutton = currentbutton;
  
  digitalWrite(led, ledon);
  
}

basically what im trying to understand is the "return current" and then down in the loop "debounce(lastbutton)"

so from what im getting here is....

we have in the debounce function "debounce(boolean last)"

so because its returning "current" at the end of the function does "last" then become "current"?

next question, in the loop now we have "currentButton = debounce(lastbutton)"

so is "lastbutton" set to the value of "last" inside the function or is it the other way around eg. "last" in the function takes the value of "lastbutton"?

complicated question here for me so im not sure im asking the correct question.

If some one can help me figure this out cause its driving me mental that would be awesome.

The return statement terminates the execution of a function and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can also return a value to the calling function.

next question, in the loop now we have "currentButton = debounce(lastbutton)"

so is "lastbutton" set to the value of "last" inside the function or is it the other way around eg. "last" in the function takes the value of "lastbutton"?

What happens is that the debounce function is called and passes the variable lastbutton to it. When the function returns the value of the variable in the return statement ( that is the variable called current ) is stored in the variable currentButton.

I will try to explain the program step-by-step, from the beginning of loop() function, maybe like this you can understand what you are asking:

void loop()
{
  currentbutton = debounce(lastbutton);

the function debounce is called. It have a parameter the value lastbutton that goes inside the function. At the first calling lastbutton have the value of the initialization, so, lastbutton = LOW.

 boolean debounce(boolean last)
   {
    boolean current = digitalRead(button);

Inside de function the value of the parameter have a different name. It is called last. The digital input is read and the value is stored in the variable current.

    if (last != current)

If both values are different (the value of the parameter and the value read) then, the following block is executed.

     {
       delay(5);
       current = digitalRead(button);
       
       return current;
     }

Wait 5 milliseconds and read again the digital input. End the execution of the function and return for the previous function with the value current.

  currentbutton = debounce(lastbutton);

In the loop() function the program starts from the point where the function was called. Now the variable currentbutton have the value returned by the function debounce.

  if (lastbutton == LOW && currentbutton == HIGH)
  {
    ledon = !ledon;
  }

The value of lastbutton never change from the beginning of the program the value currentbutton was the value read from the digital input. If they are like the if wants, then the value of the variable ledon will toggle.

  lastbutton = currentbutton;

The value of the variable lastbutton will change and will store the value that is in currentbutton. Variable currentbutton don't change.

digitalWrite(led, ledon);

The value of ledon is writed to the LED itself.

From this point, the function loop() will be repeated. So it starts from the beginning:

void loop()
{
  currentbutton = debounce(lastbutton);

Now the function debounce will be called another time, now with a different value in lastbutton.

etc., etc., etc.

I don't know if with this more clear.

EDIT: I only see this before, but your program, or to be more correct your function debounce() is not very well done. You need ALWAYS to return something, and in the case of your function, in case that last is equal to current you don't return nothing! Your function must be done like this:

boolean debounce(boolean last)
{
    boolean current = digitalRead(button);
    if (last != current)
     {
       delay(5);
       current = digitalRead(button);
     }       
    return current;
}

this actually helped quite a bit here.

so basically what i got from your explanation is

"lastbutton" begins as low, so in the function debounce(boolean last) is = to debounce(low) so last with in the function registers a s low until changed?

and about the poor coding i got it out of a book im learning out of lol.

The code is wrong.

Your conclusion is correct, but...

florios:
(...) last with in the function registers a s low until changed?
(...)

The "until" part is wrong. In the function is LOW (and don't change). Outside the function it can change in the instruction:

  lastbutton = currentbutton;

if currentbutton is HIGH, and the next time the function runs the variable last will be HIGH and will be like this. Conclusion: it only can change outside the function.

well i didn't necessarily mean it would change in the function, it would change in the next loop once i have let go of the button right?

anyways i think i have this down thanks a lot guys i appreciate it.

florios:
well i didn't necessarily mean it would change in the function, it would change in the next loop once i have let go of the button right?

OK. That sounds better, but only assuming you have the button pressed (and in this position the digital input is LOW, and when you let it go it become HIGH). I think is this that you're thinking, this is only to confirm.