recursive functions. recursion

We explain what a recursive function is and how to implement recursion or make recursive functions in a programming language.

Within the one that we have been publishing on .com, we are going to see one of the first things they teach in the creation of algorithms: recursion.

As a general definition, we can say that a recursive function is one that calls itself to resolve itself. Put another way, a recursive function is resolved with a call to itself, changing the value of a parameter in the function call. Through the successive recursive calls to the function, values ​​are obtained that, when computed, are used to obtain the value of the originally called function.

The recursive call process always has to end in a function call that is resolved directly, without the need to call the function again. This will always be necessary, so that there comes a time when the repetitive calls to the function are cut off and an infinite loop of calls is not entered.

Perhaps in theory it is more difficult to see what a recursive function is than in practice. A typical example of recursion would be the factorial function. The factorial is a mathematical function that is solved by multiplying that number by all the natural numbers between it and 1.

For example, factorial of 4 is equal to 4 * 3 * 2 * 1. If you look, for the factorial of 4 example (factorial is expressed mathematically with an exclamation point down, like 4!), it can be solved as 4*3! (4 * factorial of 3). That is, we can calculate the factorial of a number by multiplying that number by the factorial of that number minus 1.

See also  Mootools Manual

n! =n*(n-1)!

In the case of the factorial function, we have the basic case that factorial of 1 equals 1. So we can use it as a breakpoint for recursive calls.

So, we are going to code the factorial recursive function. Let’s first look at some pseudocode:

factorial function(n)
if n=1 then
factorial = 1
otherwise
factorial = n * factorial(n-1)
end function

Now let’s see how this function would be implemented with the Javascript programming language:

function factorial(n){
if(n==1)
return 1
else
return n * factorial(n-1)
}

As you can see, recursion does not represent any difficulty and in fact it is a very useful tool for algorithm programming. In web development .com we have published in various places functions that work recursively. I understand that at first it can be difficult to understand or know when to use, but when we master the concept we will see that it is an excellent way to solve problems with any programming language.

There are many algorithms that only solve with recursion, or at least whose more direct and elegant resolution is based on performing recursive functions, which call themselves to obtain the final result. For example, the traversal of various data structures, such as those of the tree type, is always done recursively, in order to be sure that we go through all the branches of the tree.

Reference: I give some addresses of .com articles that solve problems creating recursive functions:

Loading Facebook Comments ...
Loading Disqus Comments ...