How to stop a function?

I have a function in my script that i want to stop when a variable reaches a certain value. this is my script
pragma strict

var health : int = 100;
var alive : boolean = true;

function OnTriggerStay (other : Collider)
{
    if (alive == true)
    {
        health -= 2;
        yield WaitForSeconds (10);
    }
}

function Update () 
{
    if (health <= 0) 
    {
        alive = false;
        Debug.Log("dead");
    }

    if (health < 100)
    {
        Regeneration ();   
    }
    
    if (health > 100)
    {
        health = 100;
    }
}

function Regeneration () 
{
    yield WaitForSeconds (10);
    health += 2;
}

I think the problem you’re running into is that the Regeneration function is being called each frame for 10 seconds before health reaches over 100.

You can use the return command to exit a function (to answer your question), but I think re-ordering your script might be the solution. (I know I’m assuming a lot.)

Add a regeneration boolean.

var regenerate : bool = false;

Check for the bool to start regenerating only once.

if (health < 100 && !regenerate)
{
    regenerate = true;
    Regeneration ();
}

Then use a while loop in the regeneration function to keep going until 100.

function Regeneration() {
    while (health < 100) {
        yield WaitForSeconds (10);
        health += 2;
    }
    if (health > 100) health = 100;
    regenerate = false;
}