Recursion…!

Oumaymabou
1 min readFeb 17, 2021

What is Recursion?

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily.

An example of recursion would be finding the maximum value in a list of numbers :

Function find_max( list ) 

possible_max_1 = first value in list
possible_max_2 = find_max ( rest of the list );

if ( possible_max_1 > possible_max_2 )
answer is possible_max_1
else
answer is possible_max_2
end

end

Why Recursion Works?

In a recursive algorithm, the computer “remembers” every previous state of the problem. This information is “held” by the computer on the “activation stack” (inside of each function's workspace).

Every function has its own workspace PER CALL of the function.

Loops and Tail Recursion!

Some recursive algorithms are very similar to loops. These algorithms are called “tail-recursive” because the last statement in the algorithm is to “restart” the algorithm. Tail recursive algorithms can be directly translated into loops.

More explanation!!!

--

--