| | |
Iteration vs recursion
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If this is the original function
Then you can do this:
C++ Syntax (Toggle Plain Text)
int foo(int x) { while(x < 10) x = x + 1; return x; }
Then you can do this:
C++ Syntax (Toggle Plain Text)
int foo(int x) { if(x < 10) { x = x + 1; foo(x); } return x; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
@ Ancient Dragon, the recursive function is incorrect. foo(x) has to return int. The correct version would be
C++ Syntax (Toggle Plain Text)
int foo(int x) { if(x < 10) { x = x + 1; return foo(x); } return x; }
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
![]() |
Similar Threads
- finding larger number with recursion (C++)
- recursion - how to calculate sum? (C++)
- When is a return statement mandatory? (C)
- pyramid function (Python)
- iterative v/s recursion ... (C)
- Need help with recursion and arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: FindWindow() won't find the handle I'm looking for
- Next Thread: help me please I try to make a arithmetic calculator but i have 3 errors
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






