Can someone explain the recursion in C? [closed]

-1

I am taking a course in udemy about programming in C and I am going through the theme of recursion . It turns out that I'm getting a little complex, but it's mainly because of the concept or its function as such that I can not understand it, and to "put the cherry on the cake" they gave me an example of recursion with the Fibonacci series .. So if someone can please explain to me in detail what is its function and a simple concept , it would be perfect.

    
asked by Luis Rojas 23.06.2018 в 01:40
source

2 answers

0

Assuming you know the theory of recursion (basically it is a function that calls itself), its utility is multiple, but is generally used to travel multiple paths (another way of saying it, it would be to go through data ordered in tree shape, with branches that form an undefined number of branches). For example, to access all the positions of a matrix of unknown and variable dimensions.

I'll give you an example in javascript (yes, it's not C, so you can see the result here, and yes, it's not Fibonacci because I think it does not reflect its "practical" utility), a function to look for a value (the first you find) of a matrix of variable dimensions.

	var datos=[1,2,[3,4,5,[6,7],8],9];
	
	function busca(valor,matriz){
	var posicion=[];
	for (var i=0; i<matriz.length ; i++){
		if (!Array.isArray(matriz[i])) { // Si el valor NO es otra matriz.
			if (matriz[i]==valor) { 
				posicion.push(i);
				break;
			}
		} else { // Si el valor es otra matriz.
			posicion=busca(valor,matriz[i]);
			if (posicion.length>0) {
				posicion.unshift(i);
				break;
			}
		}
	}
	return posicion;
}

	for (var i=1; i<=9 ; i++) document.write("Posicion de ",i,": ",busca(i,datos),"<br>");


Note: I have decided not to use Fibonacci as an example, because I have understood that you have problems to understand in what cases it is used, and being true that Fibonacci is a typical and easy example of the mechanics of recursion, it does not show its usefulness "practical" or "realistic" since the problem is solvable perfectly with a simple loop.
For example, a loop that calculates the first 10 numbers:

var resultado=0;
var resultadoAnterior=0;
var temp=0;
for (var i=0; i<=10; i++){
  if (i<2) resultado=i; //fibo(0)=0, fibo(1)=1
  else {
    temp=resultado;
    resultado+=resultadoAnterior;
    resultadoAnterior=temp;
  }
  document.write(resultado,",");
}

On the other hand, the example I have explained, can only be solved recursively, so I think it is a more practical and realistic example.

    
answered by 23.06.2018 / 02:46
source
1

A recursive function in general is (not only in C) a function that calls itself (resorts to itself).

The first call puts the initial state then the function calls itself with the changed data (depending on the previous state), it is important to put an exit condition or else it will run ad infinitum (in practical terms until it is the memory ends).

In the case of the Fibonacci Succession

  

The sequence begins with the numbers 0 and 1 and from these, "each term is the sum of the previous two", is the recurrence relation that defines it.

To calculate the nth element of the Fibonacci sequence, the simplest algorithm is usually the " downstream recursive version "

in C:

int fibo(int num)
{
    if (num == 0) // <- condición de salida #1
    {
        return 0;  
    }
    else if (num == 1) // <- otra condición de salida #2
    {
        return 1;
    }
    else
    {
        return(fibo(num - 1) + fibo(num - 2)); // <- llamada recursiva #3
    }
}

example: find out the 8th Fibonacci number

fibo(8)
  num vale 8
  por el return fibo(8) va a ser igual a (fibo(7)+fibo(6))

fibo(7)
  num vale 7
  por el return fibo(7) va a ser igual a (fibo(6)+fibo(5))

fibo(6)
  num vale 6
  por el return fibo(6) va a ser igual a (fibo(5)+fibo(4))

fibo(5)
  num vale 5
  por el return fibo(5) va a ser igual a (fibo(4)+fibo(3))

fibo(4)
  num vale 4
  por el return fibo(4) va a ser igual a (fibo(3)+fibo(2))

fibo(3)
  num vale 3
  por el return fibo(3) va a ser igual a (fibo(2)+fibo(1))

fibo(2)
  num vale 2
  por el return fibo(2) va a ser igual a (fibo(1)+fibo(0))

fibo(1)
  num vale 1
  por el return (condición de salida #2) va a ser igual a 1

fibo(0)
  num vale 0
  por el return (condición de salida #1) va a ser igual a 0

Now we see the chain of returns

fibo(0) = 0
fibo(1) = 1
fibo(2) = 1 + 0 = 1
fibo(3) = 1 + 1 = 2
fibo(4) = 2 + 1 = 3
fibo(5) = 3 + 2 = 5
fibo(6) = 5 + 3 = 8
fibo(7) = 8 + 5 = 13

and finally return to the main with

fibo(8) = 13 + 8 = 21
    
answered by 23.06.2018 в 02:50