Difference between revisions of "Recursion"

esse quam videri
Jump to: navigation, search
(Explanation)
 
Line 1: Line 1:
  
 
=Definition=
 
=Definition=
 
+
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.
=Relevance=
 
  
 
=Example=
 
=Example=
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.
+
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.
 
<syntaxhighlight lang ="csharp">
 
<syntaxhighlight lang ="csharp">
 
void doubleNumber(int targetNumber, int minimumTarget)
 
void doubleNumber(int targetNumber, int minimumTarget)

Latest revision as of 17:36, 27 August 2019

Definition

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.

Example

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.
    
}

Resources

See also

Notes

External Links