473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Recursive call to myfunc(int x) - printing backwards?

hello every1.
i have got this code from somewhere but i can not understand its output
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void myfunc(int x)
  3. {
  4.    if(x>0)
  5.       myfunc(--x);
  6.    printf("%d,",x);
  7. }
  8.  
  9. int main()
  10. {
  11.    my func(5);
  12.    return 0;
  13. }
  14.  
The output is 0,0,1,2,3,4,. According to my logic the output should be 4.
And one more thing i am a little confuse about the last line of the code that is return 0; plz explain what this return means.
thanx in advance.
Nov 19 '07 #1
5 5519
sicarie
4,677 Expert Mod 4TB
It's a recursive function, each time passing x, and then decrementing it when it comes back. (So it's called with 5, but then goes back down to 4, 3, 2, 1, etc... and to 0 where it finally fails the condition, and then is printed out on the way back up, as many times as it was called.)

The return statement is the standard in both C and C++ for the return of int main(). It tells the compiler that everything ended correctly. There are different return codes for different scenarios (most of them are errors).

PS - Per the Posting Guidelines, SMS/'leet speak (plz, thx), are not proper English and should not be used. Thanks
Nov 19 '07 #2
It's a recursive function, each time passing x, and then decrementing it when it comes back. (So it's called with 5, but then goes back down to 4, 3, 2, 1, etc... and to 0 where it finally meets the condition, and then is printed out on the way back up.)

The return statement is the standard in both C and C++ for the return of int main(). It tells the compiler that everything ended correctly. There are different return codes for different scenarios (most of them are errors).

PS - Per the Posting Guidelines, SMS/'leet speak (plz, thx), are not proper English and should not be used. Thanks
Okay. I will take care about that from now.
But in that program the output should be 4,3,2,1,0,. (If i am not wrong)
Please correct me if i am wron.g
Nov 19 '07 #3
sicarie
4,677 Expert Mod 4TB
Well, the concept is there, but the finer points are fuzzy. Look at the code:

Expand|Select|Wrap|Line Numbers
  1. void myfunc(int x)
  2. {
  3.    if(x>0)
  4.       myfunc(--x);
  5.    printf("%d,",x);
  6. }
  7.  
So let's trace this out
Expand|Select|Wrap|Line Numbers
  1. myfunc(5)     (the call is made and passed a value)
  2. if 5 > 0      (the if statement is executed)
  3. true          (expression results in true condition)
  4. myfunc(--5)   (so function is called with new value. This is the pre-decrement operator, so it's done before the call is made, giving)
  5. myfunc(4)     (at this point, the print statement hasn't been called due to the recursive function call)
  6. if 4 > 0
  7. true
  8. myfunc(--4)
  9. myfunc(3)
  10. blah blah blah until
  11. myfunc(0)
  12. if 0>0
  13. false            (false condition finally allows if statement to stop executing, and the next call after the if statement is)
  14. print 0          (value is printed)
  15.  
Then, you have to realize that the call returns itself to it's calling function. This is myfunc(--x), and as the x with the 0 value was local to that specific instance of that call to recursive myfunc(), the x value of the myfunc() that called it was, well, 0. but the next one was 1. And so each returns from the recursive myfunc(), and moves on to the print statement.


Does that make sense? Because the print statement is after the recursive call, each separate recursive call has to return for the print statement to occur. It counts down 4,3,2,1,0, but then counts back up as the calls return.
Nov 19 '07 #4
Well, the concept is there, but the finer points are fuzzy. Look at the code:

Expand|Select|Wrap|Line Numbers
  1. void myfunc(int x)
  2. {
  3.    if(x>0)
  4.       myfunc(--x);
  5.    printf("%d,",x);
  6. }
  7.  
So let's trace this out
Expand|Select|Wrap|Line Numbers
  1. myfunc(5)     (the call is made and passed a value)
  2. if 5 > 0      (the if statement is executed)
  3. true          (expression results in true condition)
  4. myfunc(--5)   (so function is called with new value. This is the pre-decrement operator, so it's done before the call is made, giving)
  5. myfunc(4)     (at this point, the print statement hasn't been called due to the recursive function call)
  6. if 4 > 0
  7. true
  8. myfunc(--4)
  9. myfunc(3)
  10. blah blah blah until
  11. myfunc(0)
  12. if 0>0
  13. false            (false condition finally allows if statement to stop executing, and the next call after the if statement is)
  14. print 0          (value is printed)
  15.  
Then, you have to realize that the call returns itself to it's calling function. This is myfunc(--x), and as the x with the 0 value was local to that specific instance of that call to recursive myfunc(), the x value of the myfunc() that called it was, well, 0. but the next one was 1. And so each returns from the recursive myfunc(), and moves on to the print statement.


Does that make sense? Because the print statement is after the recursive call, each separate recursive call has to return for the print statement to occur. It counts down 4,3,2,1,0, but then counts back up as the calls return.
Thanks, i got it mow. It was really helpful. Thanks again.
many regards
Nov 19 '07 #5
sicarie
4,677 Expert Mod 4TB
Thanks, i got it mow. It was really helpful. Thanks again.
many regards
Glad I could help. I see you've already posted again - we're more than happy to help. Also, if you could help us and take a look at the Posting Guidelines, a lot of the stuff we ask you to do is in there - including how to form your question so it is answered quickly. Thanks!
Nov 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: krish | last post by:
What is int primitive type. Are primitive types like int , char , float are object. This is question asked to me? Can any one help me? Regards Krish.
2
by: Roli79 | last post by:
Hello together, i'm trying to call some functions wirtten in c# and compiled as a type library (dll) trough the windows cmd shell. Is this generally possible? My first idea was it to use...
2
by: Maziar Aflatoun | last post by:
Hi, Is there way in C# (or C#.net) to just define a function without having to initialize the class every time. For instance a function that takes a number, adds 2 and multiplies by 10. I know...
5
by: Stuart | last post by:
Ok let me explain: I am writing a c# program that calls into an unmanaged C++ third-party DLL. I have to make a number of calls and for simplification the protype I am calling is: bool fn(int...
3
by: bb | last post by:
I have a windows network device driver written in c++ and a user interface im porting to c#, my problem is i dont seem to be getting notified of the event calls from the driver to the c# app im...
0
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
2
by: Andrew Isaverdian | last post by:
void MyFunc(int* const ptr) { ptr = new int; ........ } In program int main() {
4
by: Randall Parker | last post by:
Have some dumb const questions even though I've been programming in C and C++ for a long time. Given that the person whose code I took over cast away const rather than figure out the answer I do...
37
by: Army1987 | last post by:
Is that in the object line a conforming program? If so, why? If not, why? I'd expect it to be much like int main(void) { for (;;); } But if I compile it with lcc-win32 and run it in its...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.