Home > Mobile >  Recursion works in reverse, then how this program printing data in sequence?
Recursion works in reverse, then how this program printing data in sequence?

Time:01-09

Recursion is always trouble to me. I understand that when we use recursion we are basically creating some stack and after creating all of the stack, recursion works in reverse order then print the result.

I am trying to understand this program bellow

#include<stdio.h>

//function declaration
int RecursvFcnTogetPrcntge();

float Var;
unsigned int count=1;
float Prcntge;

int main()
{

    printf("\nEnter a value to split in percentage: ");
    scanf("%f",&Var);

    Var=Var/100;
    RecursvFcnTogetPrcntge();


    return 0;
}

int RecursvFcnTogetPrcntge()
{

    if(count==3)
    {
        return 1;
    }

    Prcntge=Var*count;
    printf("\n= Percent = %.02f",count, Prcntge);
    count  ;
    RecursvFcnTogetPrcntge();
    return 0;
}

We have count 3, which means we are going to create 4 stacks of memory. 1 for main, 2 for else, 1 for if (count == 3) then return 1.

if it goes to the last stack(where count is 3) then it will say us to return 1. Now it returns 1 and we are in the third stack, in the third stack there is a print which supposed to print count 2 and percentage of the number. after the third stack it will come to second and first stack respectively

Therefore, the output should look like

  2 Percent = 0.14
 1 Percent = 0.07

but why it is outputting

 1 Percent = 0.07
  2 Percent = 0.14

if its recursion work backward then how its calling the function in a sequential order?

I am not maybe clear about the idea but this is what I understand about the recursion. I will appreciate any help. I in trouble with this.

CodePudding user response:

Recursion does not work in "reverse", it works in whatever order you call the function recursively. In this case you call print before you call the function recursively

int RecursvFcnTogetPrcntge()
{

    if(count==3)
    {
        return 1;
    }

    ...
    printf("\n= Percent = %.02f",count, Prcntge); // THIS COMES FIRST
    ...
    RecursvFcnTogetPrcntge(); // THIS COMES SECOND
    ...
}

So it will print the current frame of recursion, and then move to the next frame. If you had put a print statement after the recursive call, you would see it execute in reverse.

Recursion isn't special, it's just like any other function call. When you call it, it executes the function completely, and then returns. It's just that sometimes that recursive call can call itself again and again. Anything before a call will execute before the call, and anything after the call will execute after the call

Also, while, if, for, and else statements don't create stack frames.

CodePudding user response:

Given:

typedef struct node
{
    int value;
    struct node* next; // a null value here marks the end of the list.
} Node;

Consider the following two recursive functions, each of which prints a linked list.

// prints list in forward order;
void print(Node* list)
{
    if (node == null) return;
    printf("%d", list->value);
    print(list->next);
}

void printReverse(node* list)
{
    if (node == null) return;
    printReverse(list->next);
    printf("%d", list->value);
}

The first function prints the value of the argument, and then recursively prints the rest of the list. It prints the node values in forward order, from the first node to the last.

The second function recursively calls itself, creating stack frames as it goes, and then, when it reaches the end of the list, prints the value of each node as the stack unwinds (in reverse order).

Recursion will work in either direction.

  • Related