esse quam videri
Jump to: navigation, search


Recursion is the the process in which a function calls itself within the body of the function. An important step when creating a recursive function is to implement an "exit condition", a way to stop the function from infinitely repeating.


Below is an example of a recursive function that will take a number and multiply it by two. The second argument is an "exit condition" that is used to stop the function from repeating forever.

void doubleNumber(int targetNumber, int minimumTarget)
    targetNumber *= 2; // This will set the variable to itself * 2

    if(targetNumber < minimumTarget) // Check if the desired minimum has been reached. If no, execute again!
        doubleNumber(targetNumber, minimumTarget); // This function calls itself (That's the recursive part!)
    // If the above conditional statement doesn't execute, the recursion will end.


See also


External Links