* Defined Recursion Added an English definition for recursion with examples and external sources. * md-ize and format
		
			
				
	
	
	
		
			2.3 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	title
| title | 
|---|
| Recursively Defined Functions | 
Recursively Defined Functions
This is a stub. Help our community expand it.
This quick style guide will help ensure your pull request gets accepted.
A Quick Intro
Put simply, recursion if a form of defining something using itself within the definition
This is a common example used to teach recursion, a function to generate the fibonacci numbers:
f(0) := 0  Base case 0
f(1) := 1  Base case 1
f(x) := f(x - 1) + f(x - 2) | x > 1
Now, defining a function with itself will leave you with a few problems. The biggest is that if you define a function with itself, how are you supposed to know/determine when the function stops?
Here's a simple example:
function foo(x){
  y = x + 1;
  console.log(y);
  foo(y);
}
This function takes some number, and adds 1 to it infinitely. In order to determine a stopping point, you need to add a base case.
A base case is a point at which the function does not call the recursive step.
function foo(x){
  if(x > 5){
    return x
  }
  y = x + 1;
  console.log(y);
  foo(y);
}
This is now equivalent to:
function foo(x){
  while(y < 5){
    y = x + 1;
    console.log(y);
  }
}
Almost everything that can be defined recursively can also be created with a loop. In fact, most compilers turn your code into a loop anyway, at some point, prior to it being executed.
It should also be noted that recursion may look cool and allow you to shrink a long loop down into just a few lines of code but there are a few drawbacks. Mainly, performance of a recursive definition is generally slower than that of an iterative one, not to mention, it can be harder to read for people unfamiliar or new to the concept.