How do I cancel a function that is running using a button?

Hi guys! I made two functions using button. It's a functions that i do with just one button.

I want to stop running by pressing the button while the function is running. But I don't think I should use the delay() function. I think maybe i should use the millis() function, but I don't know much about millis().

For example, in the code below, when Function 1 is running, I want to cancel it by pressing the button and execute Function 2. How should I write the code?

I'd appreciate your advice. Thank you!

void Function_1(){ //기능1 : LED1 3초 ON
  digitalWrite(led1, HIGH);    
  delay(3000);
  digitalWrite(led1, LOW);     
}

void Function_2(){ //기능2 : LED2 5번 깜빡임.
  for(int i = 0; i <5; i++){
    digitalWrite(led2, HIGH);    
    delay(500);
    digitalWrite(led2, LOW);    
    delay(500);    
  }
}

First, learn to use millis() instead of delay(...). It is quite challenging to check for a button while delay(...) is executing. delay(500) means that you may have to hold the button for one half second. delay(3000) means that you may have to hold the button over three seconds! The delays could be broken up into smaller pieces with more frequent checking, but at that point it would be simpler to use millis() anyway.

The below steps explain the process to get to a millis() based approach. Demonstrated for Function_2(), I'll leave the other function to you.

In a first step, replace the for-loop

void Function_2()
{
  // counter replacing i of the for-loop
  static byte counter = 0;

  digitalWrite(led2, HIGH);   
  delay(500);
  digitalWrite(led2, LOW);   
  delay(500);   

  // increment the counter
  counter++;
  // if the required number of iterations is completed
  if(counter == 5)
  {
    // reset the counter
    counter = 0;
  }
}

This uses a static local variable; static local variables are like global variables (so they will be remembered), but only known to the function.
If you simply call this function from loop(), the result will behave the same as the original. E.g.

void loop()
{
  Function_2();
}

Next you need to implement a millis() based approach

void Function_2()
{
  // counter replacing i of the for-loop
  static byte counter = 0;
  // variable to keep track of the start time of a delay
  static unsigned long delayStartTime;
  // variable to keep track of the ledState
  static byte ledState = HIGH;

  // check if it's time to change the led
  if(millis() - delayStartTime >= 500)
  {
    // change the led
    digitalWrite(led2, ledState);
    
    // set a new start time for the delay
    delayStartTime = millis();
    // change the ledState for the next time that we need to change it
    ledState = !ledState;
  }

  // increment the counter
  counter++;
  // if the required number of iterations is completed
  if(counter == 10)
  {
    // reset the counter
    counter = 0;
  }
}

Note that the couter now goes to 10 (5x on, 5x off).

Sometimes you might want to know if a complete 'for-loop' is completed. The function can return a bool variable to indicate this.

/*
  blink led2 every 500ms
  Returns:
    true if blink cycle completed, else false
*/
bool Function_2()
{
  // counter replacing i of the for-loop
  static byte counter = 0;
  // variable to keep track of the start time of a delay
  static unsigned long delayStartTime;
  // variable to keep track of the ledState
  static byte ledState = HIGH;

  // check if it's time to change the led
  if(millis() - delayStartTime >= 500)
  {
    // change the led
    digitalWrite(led2, ledState);
    
    // set a new start time for the delay
    delayStartTime = millis();
    // change the ledState for the next time that we need to change it
    ledState = !ledState;
  }

  // increment the counter
  counter++;
  // if the required number of iterations is completed
  if(counter == 10)
  {
    // reset the counter
    counter = 0;
    // indicate to caller that a full cycle was completed
    return true;
  }
  else
  {
    return false;
  }
}

In loop(), you can now read buttons without having to wait for a cycle to be completed.

The below demonstrates how the return value can be used. It will do one cycle of 5 flashes and after that hang forever.

void loop()
{
  bool rc = Function_2();
  if (rc == true)
  {
    for (;;);
  }
}

All this migt need some polishing depending on your exact needs.