How does this C programme work with recursive functions?

  • Thread starter Thread starter rclakmal
  • Start date Start date
  • Tags Tags
    Explain
Click For Summary

Discussion Overview

The discussion revolves around understanding a C program that utilizes recursive functions. Participants are attempting to clarify how the program executes and produces its output, specifically focusing on the flow of control and the concept of recursion.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant requests an explanation of the C program and expresses confusion about its output.
  • Another participant suggests that tracing the execution of the program by hand would be more beneficial than receiving an explanation.
  • A participant describes their dry run of the program, detailing how the function calls occur and expressing confusion about how the numbers 1, 2, 3, and 4 are printed after 0.
  • Another participant prompts the original poster to consider the flow of control and asks if they are familiar with recursion.
  • A participant emphasizes the importance of understanding the execution stack and how each function call creates a separate frame, which may not have been fully grasped by the original poster.
  • A later reply provides a simplified example of the function calls to illustrate how the recursion works, using a call to func with the argument 2 to demonstrate the concept of separate variable instances at each level of recursion.

Areas of Agreement / Disagreement

Participants generally agree on the need for a clearer understanding of recursion and the execution stack, but there is no consensus on the best way to explain the program's behavior. The discussion remains unresolved regarding the original poster's confusion.

Contextual Notes

Some participants note the complexity of tracking multiple function calls and frames, which may contribute to the confusion experienced by the original poster. There is an acknowledgment of the challenge posed by the program's recursive nature.

rclakmal
Messages
76
Reaction score
0
explain this C programme??

#include<stdio.h>
void func (int x);
void func(int x)
{
if (x>0)
func(x-1);
printf("%d",x);
}
int main()
{
func(4);
return 0;
}

this C programme gives the output of

0 1 2 3 4

can anyone explain me how this happens I am totally confused ...
 
Technology news on Phys.org


No explanation will be nearly as useful as you working it out yourself. Trace the execution of your program by hand. What is the very first thing it does when it's started?
 


yr i tried like that ,...but according to my dry run

first function get 4 and its greater than 0 and coz of that function get 3 like wise it goes until 1 then when the function get 1 and as it is also greater than 0 function get 0.Then 0 is NOT greater than 0 .so if loop is skipped then the 0 will be printed .but how on the Earth these 1234 gets print out ....i don't how it happens please help me ...please ignore my dumbness if this is a very easy one ...:D
 


Think about flow of control. How do these functions get called exactly? Ever heard of recursion?
 


Need more explanation .!Badly needed...!yr of course i have heard about recursion ...its about calling a function inside it self which is same as here ...but can u please help me with the dry run ...it will be a great help ...coz I am doing 6 subjects and don't have enough time to think about 1 for a long ...exam is coming on the way ...any help would be greatly appreciated.thanks!
 


Speak English, please; save the txtk for texting.


I think you haven't internalized the notion of an execution stack. You've realized that func gets called five times, but it looks like you've mentally merged all of them together.

But that's not what happens -- each of the five calls to func give five completely different "frames", and you have to keep track of them all and keep them separate.
 


Here's a simple example... call func with 2.

Code:
func(2)
...x=2
   x>2?
      yes, so...func(2-1)
                   ...x=1 ... this is a new "x" variable for this level.
                   ...x>0?
                         yes, so...func(1-1)
                                      ...x=0 ... this is another new "x" variable.
                                         x>0? no.
                                         print "0"
                   print "1"... remember, x was "1" at this level.
   print "2" ... again, x was "2" at this level.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 17 ·
Replies
17
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
14
Views
4K