Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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     |
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading