What is recursion ?

What is recursion ?

Is the process or concept in programming where the function calls itself, we use recursion cause for certain problems it will give you a better solution, more effective and you are able to reuse code, lest's see the following example to solve pow.

In c languages for example we need to iterate to accomplish several multiplications, so you do it through a while or for loop, so that means we can use recursion, before we start we need to understand that the power of a number represents the amount of times the number itself needs to be multiplies, three power of two is equal to 2 ^ 3 = 2*2*2.

In order to see how this works inside memory, we are going to talk about the stack , in the following imagen we can se how is our program and variables are located at .

memory ram

So float x and float y , will be located at Static Variables space, that means every time we make a call of the function _pow_recursion() those variables with the new value are going to be there , as well the code .

Les't now see who it works we going to assign the value of 3 to x and the value of 2 to y,

x = 3 y = 2 now, when the function stops to calls itself ? well when the variable y reaches zero.


No hay texto alternativo para esta imagen


Now what happen if our float y is negative y = -2 , so we will call the function with the following line : _pow_recursion(x, y + 1) / x; is the same process but this time instead to subtract it, We will de adding it (y + 1), every time we do a new call it creates on the heap new variables with the new value , and once it hits the return on the last line they are delete and used to computing the new value.

Thanks to share with me your learning path, or just have a good time refreshing info

I hope you enjoy it, see you on the next one !

要查看或添加评论,请登录

Laura Callejas的更多文章

  • What does STEM mean?

    What does STEM mean?

    STEM stands for science, technology, engineering, and mathematics, and it has become the foundation of both education…

  • Web stack incident report

    Web stack incident report

    In this blog, we are going to talk about software system failure, we are going to check the possible reasons for it…

  • What happens when you type google.com in your browser and press Enter?

    What happens when you type google.com in your browser and press Enter?

    In the following article we are going to answer What happens when you type google.com and press enter? so we are going…

  • IoT (The Internet of things)

    IoT (The Internet of things)

    WHAT IS IOT ? Well the Internet of things is a system of connected devices , computers and multiples digital machines…

  • Everything is an object in Python!

    Everything is an object in Python!

    Python is an object-oriented programming language, and in Python everything is an object, an object is an entity that…

  • Static libraries vs Dynamic libraries.

    Static libraries vs Dynamic libraries.

    Libraries are a collection of files that contains functions in this case with extension .c for example stdlib.

  • Static Libraries in C .

    Static Libraries in C .

    Static library is a set of routines, external functions and variables which are resolved in a caller at compile time…

  • What is GCC ?

    What is GCC ?

    It is a compiler for GNU considered standard for operating system system derived from unix.The acronym GCC stands for…

社区洞察

其他会员也浏览了