How to "break" a function we made? This is code about a keypad lock. The user can unlock the device by entering the secret code. To lock it again they press "#" or "*".  

I want to let the green LED turn on for about 0.5 seconds then return to the first line of the loop: char key = keypad.getKey();. 

The problem is that this code makes the green LED turn on forever after we enter the correct secret code or until we enter "#" or "*".

void loop() {
  char key = keypad.getKey();
  if (key=='*'||key=='#') {
    position = 0;
    setLocked(true);
  }
  if (key==secretCode[position]) {
    position ++;
  }
  if (position ==3) {
    setLocked(false);
  }
  delay(100);
}

void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
  }
}
I may be missing something, but couldn't you just modify your setLocked function?

void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  }
  return;
}