Difference between revisions of "Recursion"
esse quam videri
(Created page with " =Definition= =Relevance= =Explanation= =Resources= == See also == ==Notes== ==External Links== Category:Programming Language Concepts Category:Object Orient...") |
|||
(One intermediate revision by the same user not shown) | |||
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. | ||
− | = | + | =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. | |
− | = | + | <syntaxhighlight lang ="csharp"> |
+ | 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. | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
=Resources= | =Resources= |
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.
}