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

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%3d 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.

1

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%3d 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.

1

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.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật