user16381: Edit 2 
I made a function that works, but I'm still confused about just one thing...
I'm very confused about how variables work in C++.
In this program...
  boolean debounce(void)
{
  static boolean buttonState=LOW;
  static boolean lastButtonState;
  boolean currentState = digitalRead(4);
  static unsigned long lastDebounceTime;
  if (currentState != lastButtonState){ //if the button state has changed
    lastDebounceTime=millis();  //reset timer
    }

  if ((millis()-lastDebounceTime) > 50)  //if 50 milliseconds has passed since last bounce
  {  
    buttonState = currentState;  //read value again now that bouncing is over

    }
  lastButtonState = currentState;
  return buttonState;
} 

Why does changing this
lastButtonState = currentState;

To this
lastButtonState = buttonState;

Make any difference in the program?
When I try the latter line of code, the whole function stops working and I don't understand why.

user16379: Im not an expert of C programming, but I can see lot of troubles in your programming, because you do call a function debounce(), but it is not a function that does a complete task, so you set additional variables outside of it. This is a bad practice, because you can't call the same function to compute multiple keys, that's what functons are for - make one function and call multiple times with different parameters. You should switch to pointer orented parameters for lastButtonState.
    boolean LastButton_1; // global or static variable
    long LastDebounce_1;
    int BUTTON_1;
        ... and so on



    boolean debounce(boolean *LastButton, long *LastDebounce, int BUTTON)
        {
          //your code here, where you read and write to variables located at pointer
           boolean something, new_value;
           something = *LastButton; //read value at pointer address
           *LastButton = new_value; //store the value at pointer address
        } 
//now you call this
debounce(&LastButton_1, &LastDebounce_1, BUTTON_1)

Perhaps it is not correct, since I know only very basic C, but the way is the right one

user614: "It worked perfectly" is probably an overstatement. Yes, you saw the LED change when you pressed the button, but that doesn't mean it was getting debounced.
The problem is that you are trying to use global variables to remember the internal state of the debouncer function, and you're passing those variables into the function as parameters. In C (or C++, which is the underlying language that you're using), an assignment to a function parameter does NOT update the corresponding variable in the caller's environment. Therefore, lastDebounceTime never gets updated, and your debounce() function always just returns the current state of the button.

user16378: This probably won't answer your question completely, but here are some notes from me:
buttonState = current ;  //read value again now that bouncing is over

This won't read the value again, it just copies current into buttonValue.
What you wanted is:
buttonState = digitalRead(BUTTON); // read value again now that bouncing is over

You have multiple issues with your debounce call:

lastTimeDebounce is never used by your code, so it is always zero. 
lastButtonState will be always buttonVal making the debounce function useless.

Some other tips:

Don't use the same names for parameters as for global variables:
boolean debounce(boolean lastButton, long lastDebounce, int BUTTON)
The global variable BUTTON will be shadowed by int BUTTON, essentially overriding it.
Don't use global variables (unless you have a very good reason)

EDIT
I would strongly recommend writing a function that debounces only one pin. 
Because if you want a function which can debounce any pin you would need a state variable for each pin (which needs arrays hence a lot of space).
EDIT2:
Here is a debounce function that should work: 
const uint8_t PIN = 13;

/*
 * Returns debounced value for `PIN`
 */
boolean debounce(void)
{
    // Current state of the pin
    boolean currentState;
    // Last state of the pin, initial value is zero
    static boolean lastCurrentState = 0;
    // Timer
    static unsigned long debounceTimer = millis();
    // Debounced value, initial value is zero
    static boolean debouncedValue = 0;

    // We first read the current state of the pin
    currentState = digitalRead(PIN);

    // Then we look if the state has changed
    if (currentState != lastCurrentState) {
        // If it has, we need to reset the debounce timer
        debounceTimer = millis();
    }

    // Then we check if the timer has expired
    if ((millis() - 50) > debounceTimer) {
        // The timer expired so we update the value
        debouncedValue = currentState;
    }

    // Save the latest state
    lastCurrentState = currentState;

    // We always return a debounced value:
    return debouncedValue;
}

But always test such things with an oscilloscope, no guarantee. FYI: this is not an ideal debounce function, but it should make the idea clear.

16381: <p><strong>Edit 2</strong> 
I made a function that works, but I'm still confused about just one thing...</p>

<p>I'm very confused about how variables work in C++.</p>

<p><strong>In this program...</strong></p>

<pre><code>  boolean debounce(void)
{
  static boolean buttonState=LOW;
  static boolean lastButtonState;
  boolean currentState = digitalRead(4);
  static unsigned long lastDebounceTime;
  if (currentState != lastButtonState){ //if the button state has changed
    lastDebounceTime=millis();  //reset timer
    }

  if ((millis()-lastDebounceTime) &gt; 50)  //if 50 milliseconds has passed since last bounce
  {  
    buttonState = currentState;  //read value again now that bouncing is over

    }
  lastButtonState = currentState;
  return buttonState;
} 
</code></pre>

<p><strong>Why does changing this</strong></p>

<pre><code>lastButtonState = currentState;
</code></pre>

<p><strong>To this</strong></p>

<pre><code>lastButtonState = buttonState;
</code></pre>

<p><strong>Make any difference in the program?</strong></p>

<p>When I try the latter line of code, the whole function stops working and I don't understand why.</p>
 16379: <p>Im not an expert of C programming, but I can see lot of troubles in your programming, because you do call a function debounce(), but it is not a function that does a complete task, so you set additional variables outside of it. This is a bad practice, because you can't call the same function to compute multiple keys, that's what functons are for - make one function and call multiple times with different parameters. You should switch to pointer orented parameters for lastButtonState.</p>

<pre><code>    boolean LastButton_1; // global or static variable
    long LastDebounce_1;
    int BUTTON_1;
        ... and so on



    boolean debounce(boolean *LastButton, long *LastDebounce, int BUTTON)
        {
          //your code here, where you read and write to variables located at pointer
           boolean something, new_value;
           something = *LastButton; //read value at pointer address
           *LastButton = new_value; //store the value at pointer address
        } 
//now you call this
debounce(&amp;LastButton_1, &amp;LastDebounce_1, BUTTON_1)
</code></pre>

<p>Perhaps it is not correct, since I know only very basic C, but the way is the right one</p>
 614: <p>"It worked perfectly" is probably an overstatement. Yes, you saw the LED change when you pressed the button, but that doesn't mean it was getting debounced.</p>

<p>The problem is that you are trying to use global variables to remember the internal state of the debouncer function, and you're passing those variables into the function as parameters. In C (or C++, which is the underlying language that you're using), an assignment to a function parameter does NOT update the corresponding variable in the caller's environment. Therefore, <code>lastDebounceTime</code> never gets updated, and your <code>debounce()</code> function always just returns the current state of the button.</p>
 16378: <p>This probably won't answer your question completely, but here are some notes from me:</p>

<pre><code>buttonState = current ;  //read value again now that bouncing is over
</code></pre>

<p>This won't read the value again, it just copies <code>current</code> into <code>buttonValue</code>.</p>

<p>What you wanted is:</p>

<pre><code>buttonState = digitalRead(BUTTON); // read value again now that bouncing is over
</code></pre>

<p>You have multiple issues with your <code>debounce</code> call:</p>

<ul>
<li><code>lastTimeDebounce</code> is never used by your code, so it is always zero. </li>
<li><code>lastButtonState</code> will be always <code>buttonVal</code> making the debounce function useless.</li>
</ul>

<p>Some other tips:</p>

<ul>
<li><p>Don't use the same names for parameters as for global variables:</p>

<p>boolean debounce(boolean lastButton, long lastDebounce, int BUTTON)</p>

<p>The global variable <code>BUTTON</code> will be <strong>shadowed</strong> by <code>int BUTTON</code>, essentially overriding it.</p></li>
<li><p>Don't use global variables (unless you have a very good reason)</p></li>
</ul>

<p><strong>EDIT</strong></p>

<p>I would strongly recommend writing a function that debounces only <strong>one</strong> pin. </p>

<p>Because if you want a function which can debounce any pin you would need a state variable for each pin (which needs arrays hence a lot of space).</p>

<p><strong>EDIT2</strong>:</p>

<p>Here is a debounce function that should work: </p>

<pre><code>const uint8_t PIN = 13;

/*
 * Returns debounced value for `PIN`
 */
boolean debounce(void)
{
    // Current state of the pin
    boolean currentState;
    // Last state of the pin, initial value is zero
    static boolean lastCurrentState = 0;
    // Timer
    static unsigned long debounceTimer = millis();
    // Debounced value, initial value is zero
    static boolean debouncedValue = 0;

    // We first read the current state of the pin
    currentState = digitalRead(PIN);

    // Then we look if the state has changed
    if (currentState != lastCurrentState) {
        // If it has, we need to reset the debounce timer
        debounceTimer = millis();
    }

    // Then we check if the timer has expired
    if ((millis() - 50) &gt; debounceTimer) {
        // The timer expired so we update the value
        debouncedValue = currentState;
    }

    // Save the latest state
    lastCurrentState = currentState;

    // We always return a debounced value:
    return debouncedValue;
}
</code></pre>

<p>But always test such things with an oscilloscope, no guarantee. FYI: this is not an ideal debounce function, but it should make the idea clear.</p>
