Search:
Forum
Beginners
help in recursion
help in recursion
Nov 21, 2012 at 5:57am UTC
avesh
(2)
void myFunc (int x)
{
if (x > 0)
myFunc(--x);
printf("%d, ", x);
}
int main()
{
myFunc(5);
return 0;
}
can any one explain this works, as 5 is passed to myfunc it decrement until reaches 1 and as it reached zero the if fails and printf prints 0, that wt after that, i am confused.... . thanks
Nov 21, 2012 at 7:28am UTC
vlad from moscow
(6539)
If you need to print all values less or equal to x starting from zero then the function will look the following way
1
2
3
4
5
void
myFunc(
int
x ) {
if
( 0 < x ) myFunc( x - 1 );
if
( !( x < 0 ) ) printf(
"%d, "
, x ); }
Nov 21, 2012 at 7:40am UTC
avesh
(2)
thanks but when i compile it the o/p is 0 0 1 ......
how come that..
Topic archived. No new replies allowed.
C++
Information
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs