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...") |
(→Explanation) |
||
Line 4: | Line 4: | ||
=Relevance= | =Relevance= | ||
− | = | + | =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. | ||
+ | <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= |
Revision as of 14:32, 22 August 2019
Definition
Relevance
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.
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.
}