Principios de la Recursividad en Programacion
In programming there is such an important concept that can make our work easier, we talk about recursion. this concept is very dificult at first, but later, once you learn it, is very easy to apply and it can solve varius prblems.
What is recursividad in programming?
The recursividad is a concept that start when method calls itself.
We must take into account that when we call this method, hi that finish, otherwise is would be calling itself. We say the cicle is finit.
We must be very careful when making recursive calls because if we use it without control we could overflow the computer's memory, causing the program to break.
When to use recursion?
We can use recursion that sustitute any kind bucle, but in the real actual work donot using a lot, bucause a mistake can be tragic for the momory, so how to have a big list with a millions data, can use a very cant of a memory. Even so can use recursion for seach algoritm and order.
When is base case?
The base case is a condition to the finish of the recursion.
For Example:
- Calculate the factorial of a number:
Iterative method
Recursive method
The stack of the recursion
The memory of the computer is divided into four segments:
* Code segment: Save the instruccions the program in machine code.
* Data segment: Save the static or constant variable.
* Mound: Save the dinamic variable.
* Stack of program: Save the local variable and function parametrs that in ejecuted.
When call the function, the proccess is the next:
1) Space is reserved on the stack for parameters the function and her local variables.
2) Save in the stack the direction of de line of code from we method called.
3) Save the parameters of the function and the stack value.
4) Finally released the asigned memory that stack when the function finish and called the original code.
But the function is recursive:
1) Every call generate a new call in the function with the correspondent local objets.
2) Re-executing completely until the call itself. Where re-create the new parameters in the stack and local variables.
3) When finished, the memory is freed on the stack, starting from the last function created to the first, which will be the last to be freed.
Comentarios
Publicar un comentario