Created
August 23, 2009 23:16
-
-
Save sms420/173539 to your computer and use it in GitHub Desktop.
recursive function examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // recursive function examples | |
| #include<iostream> | |
| using namespace std; | |
| void printForward(int p) { | |
| if (p==0) | |
| return; | |
| cout << p; | |
| printForward(p-1); | |
| return; | |
| } | |
| void printBackward(int p) { | |
| if (p==0) | |
| return; | |
| printBackward(p-1); | |
| cout << p; | |
| return; | |
| } | |
| int main() { | |
| printForward(4); | |
| cout << endl; | |
| printBackward(4); | |
| cout << endl; | |
| return 0; | |
| } | |
| /* sample output: | |
| $ ./a.out | |
| 4321 | |
| 1234 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment