Doubt about the "return" statement in recursion - C

4

I am studying the subject of recursion and I have two doubts that consist of the following:

1 .- I have seen that normally when calling a function within itself it is done through return , something like the following:

return fibonacci(n-1) + fibonacci(n-2);

My question is if it is still considered recursion if you call without a return , for example:

void funcionRecursiva (/*Parámetros*/)
{
    //Caso base
    if (/*Alguna condicional aquí*/) return;

    /*
     *
     * Codigo
     *
    */

    funcionRecursiva (/*Parámetros*/);
}

The above function would call itself until a certain condition is met (base case), and if it is not met, it calls itself until it is fulfilled, but does not call itself with a return , does that mean that it does not "count" as recursion?

2 .- If the above does not "count" as recursion, would it count if it changes to something like this ?:

void funcionRecursiva (/*Parámetros*/)
{
    //Caso base
    if (/*Alguna condicional aquí*/) return;

    /*
     *
     * Codigo
     *
    */

    return funcionRecursiva (/*Parámetros*/);
}

Is it correct to call a function within itself using a return if that function is of void ?

I appreciate all the help and direction they could give me. Thanks.

    
asked by user35102 07.04.2017 в 03:05
source

2 answers

6

As you have been told, a recursive function is one that calls itself. He has no more.

Calling it in return is to use a compiler optimization , so the stack is reused for the call, so the stack does not will grow unnecessarily if we no longer need it more.

This optimization can only be used when we do not need to perform any calculation between the value returned by the recursive call and some internal variable of the function .

You can see Segmentation Fault - Recursion for more details.

    
answered by 07.04.2017 / 06:03
source
5

The first case yes is a recursive call, it fulfills the condition of invoking itself within its definition. The fact that it returns a value or not through return is not related to the recursion.

    
answered by 07.04.2017 в 03:45