Returns in a recursive function

nounoursnoir

I am trying to understand how to use recursion in C, and I can't get how return works in it.

Please consider the following code:

int     recur(int i)
{
    printf("recur: i = %d\n", i);
    if (i < 3)
    {
        recur(i + 1);
        return 10;
    }
    else if (i < 5)
        recur(i + 1);
    return i;
}

int     main(void)
{
    int     i = 0;
    i = recur(i);
    printf("i = %d\n", i);
    return 0;
}

The output is:

recur: i = 0
recur: i = 1
recur: i = 2
recur: i = 3
recur: i = 4
recur: i = 5
i = 10

What does the last return, return i, do? Does this code even make sense?

Vlad from Moscow

The recursive calls of the function do not influence on the returned value. Only the first return met in the first instance of your recursive function will return a value to the parent function. Any other return met will just stop the function's instance the program is currently in.

Thus as the function was called in main with the argument 0

int     i = 0;
i = recur(i);

The first return met is located inside of an if statement:

if (i < 3)
{
    recur(i + 1);
    return 10;
}

In this case, the recur function is called before returning a value to main. It will create another instance of recur which will do some stuff, but after this instance of recur has ended, the main instance of recur will continue and, in this case, will return 10 to the function main.

To know what your recursive function will return to the main function, you can simply comment all calls to a new instance of the function:

int     recur(int i)
{
    if (i < 3)
    {
        //recur(i + 1);
        return 10;
    }
    else if (i < 5)
    {
        //recur(i + 1);
    }
    return i;
}

In this case, this is what the program will read:

int     recur(int i)
{
    if (i < 3)
        return 10;
    return i;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Recursive function that returns the remainder

Python - recursive function returns None

Recursive prompt function returns null

TypeScript Recursive function returns undefined

Recursive function in Javascript returns undefined

Recursive function that returns the smallest divisor

Recursive Function Always Returns False

why this recursive function returns a tuple?

Js recursive function that specified attribute returns it

Recursive function that returns the number of possible combinations

Recursive function on sorted array returns undefined

Why this recursive javascript function returns undefined?

JavaScript: Recursive function with Promise, resolve returns "undefined"

Javascript: recursive function returns undefined for existing value

Python returns NoneType if an Except happens in recursive function

PHP recursive function returns empty JSON object

Recursive function return incorrectly returns false

Simple Recursive Javascript Function Returns Undefined

Recursive function to add noise to a polygon returns undefined

Recursive function returns None when recurred

how can a recursive function operate if it returns to the beginning?

Python recursive function returns None instead of value

Pandas Dataframe returns None after recursive function?

Boolean recursive function always returns true

Tail recursive reduce function returns [..., [Curcular] ]

recursive lua function always returns nil

Recursive function that returns array with random X values

My find-biggest-number Recursive function returns a discremented value

My Elixir recursive function returns a list of lists, not a simple list