Hello guys ,
I decided to do an example about recursion cause it's an argument I had difficulty understanding
at first.So what is recursion in programming?
Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the "process" to sometimes be completed without the recursive call. One simple example is the idea of building a wall that is ten feet high; if I want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks.
so here is an example I wrote for recursion so you can understand it better(I will post more examples as I code them)
/*rafy recursion example 1 */
#include <stdio.h>
void recurse (int count)
{
if (count<9) /*We need a limit otherwise the program would execute till it crashes*/
{
printf("%d \n",count); /*prints the count on-screen*/
recurse (count +1); /*calls the function recurse and adds 1 to the count*/
}
}
int main ()
{
recurse (1); /* the first time I call the function I put count=1 */
return 0;
}
So here it goes for now.I know tail recursion is missing,when I learn that I will add a second part to the tutorial.