algorithmrecursion

what is the output of this simple recursive function? can you explain in detail?


void test(int start)
{
    for(int i=start; i<2; i++)
    {
        printf("%d", start);
        test(i+1);
    }
}

--> test( 0 );

I know the output will be 0-1-0, but I don't understand how the last 0 come from? can anyone help me understand this please?

what if the for loop change to

for(int i=start; i<3; i++)

Thank you in advance


Solution

  • You are printing out start, but seem to be thinking about printing out i

    |Variables    | Output|
    |-------------|-------|
    |start=0, i=0 | 0     |
    |start=1, i=1 | 1     |
    |start=0, i=1 | 0     |