Okay here's a program that uses recursion (A function calling itself. In this case, the function reverse() runs, and calls itself).
I'm reading the book Programming With C by Byron S. Gottfried. It's hard to get into a book if a lot of the discussion is about stuff you know. But I'm picking up a lot of things from it.
I find it hard to think in recursion, but here's my attempt to explain how this program works.
I'm reading the book Programming With C by Byron S. Gottfried. It's hard to get into a book if a lot of the discussion is about stuff you know. But I'm picking up a lot of things from it.
I find it hard to think in recursion, but here's my attempt to explain how this program works.
First: The difference between getch(), getche() & getchar()
getch() - (Get Character?) Accepts a key-press and moves on.
getche() - (Get Character Echo?) Accepts a key-press, ECHOS (displays) it on the screen, moves on.
getchar() - (Get Character Return?) Accepts any number of key-presses, displays it on the screen. which is stored in a buffer (A space in memory) until user hits enter. The first key-press is returned. Suppose you enter "abcd" for c=getchar(); and press enter, 'a' will be stored in c. but the 'bcd<enter key>' is still in the buffer......just sitting there.
This program runs the "reverse()" function and comes to the line if((c=getchar())!='\n')reverse();
lets simplify that:
c=getchar();
if(c!='\n')
{
reverse();
}
So getchar() waits, displaying whatever you type, waiting for you to press enter. The first character of whatever you enter will be assigned to c.
Say you enter "Guns" and hit enter. "G" is assigned to c ("uns<enter key>" is sitting in the buffer), then it checks if c is the enter key, no it's not, so it calls (runs) the reverse() function again WITHIN THE ONE ALREADY RUNNING.
A new space in memory is created by 'char c'. The program encounters the if((c=getchar())!='\n')reverse(); line again. getchar() is fed "u" from the buffer. It checks if c is and enter key, nope, it's just a 'u', so it calls the reverse() function again WITHIN THE ONE RUNNING THAT IS WITHIN THE FIRST ONE THAT WAS RUN.
Yet again, as reverse() runs, it creates a new char c (same name, different space in memory), it is fed 'n'...
so on and so forth until c is finally fed the enter key '\n'.
Now when the condition is checked if(c!='\n') (if c is not equal to the enter key), but this time, c IS the enter key, so it goes on to the line putchar(c), displaying c which is an enter key in this instance, so you have a blank line (just as if you had hit enter when typing something).
Then THE LAST INSTANCE OF reverse() ends, and comes back to the previous instance which was paused at
c=getchar();
if(c!='\n')
{
reverse(); <-It had paused here, because it has called reverse()
}
Then it picks up from where it left off, going on to putchar(c) (this is the SECOND TO LAST instance where c was assigned "s", so you see an "s" on the screen. Then it ends, and the THIRD TO LAST instance picks up, putting c which in his case was assigned "n". When the entire thing unravels.......
You Enter:
Guns<Enter>
You get the output:
<Blank Line>
snuG
Sample:
Code:
#include <stdio.h>
main()
{
void reverse(void);
printf("Please enter a line of text.\n");
reverse();
return 0;
}
void reverse(void)
{
char c;
if((c=getchar())!='\n')reverse();
putchar(c);
return;
}
No comments:
Post a Comment