1. ## recursion

Hi all i have problems to understand recursion .Here are two programms.Could someone tell me how they work?

1.
Code:
```#include <stdio.h>

void up_and_down(int);

main( )
{
up_and_down(1);
}

void up_and_down(n);
int n;
{
printf ("Στάδιο %d\n", n);
if (n < 4)
up_and_down(n+1);
printf ("Στάδιο %d\n", n);
}```
2.
Code:
```#include <stdio.h>

int summing(int);

main()
{
int k=0;

k = k + summing(1);
printf("%d\n", k);
}

int summing(n)
int n;
{
int j=0;

if (n++ < 4)
j = summing(n)+ n;
return j;
}```

2. I'll just explain recursion.
Code:
```void blah(void)
{
int n = 2+2;
// THIS is recursion:
blah();
}```

3. ## recursion

1.
Code:
```#include <stdio.h>
#include <stdlib.h>
void up_and_down(int);

main( )
{

up_and_down(1);
system("PAUSE");
return(EXIT_SUCCESS);

}

void up_and_down(int n)
{

printf ("step %d\n", n);

if (n < 4) {
up_and_down(n+1);
}

printf ("step %d\n", n);

}```
Output is:

step 1
step 2
step 3
step 4
step 4
step 3
step 2
step 1

At first call of function
up_and_down(1) will print "step 1" and will call up_and_down(2) then print again "step 1"
up_and_down(2) prints out "step 2" and calls up_and_down(3)
.....
step 1 [first printf up_and_down(1)]
-step 2[first printf up_and_down(2)]
-----step 3[first printf up_and_down(3)]
---------step 4[first printf up_and_down(4)]
---------step 4[second printf up_and_down(4)]
-----step 3[second printf up_and_down(3)]
-step 2[second printf up_and_down(2)]
step 1[second printf up_and_down(1)]

In recursion a function creates a loop by calling itself within.
Condition if(n<4) makes the function to stop at the moment when n=4 [recursion stops]

Runs first printf then it is call up_and_down(n+1) wich will run again first printf, after it finishes, second printf will run from last call [n, n-1, ...1]

4. No one is going to tell you how a program works (especially if there are errors in the code for said program). Do a board search for posts by HelpMePlease and you'll find all about this...

The idea behind recursion is that if you have an algorithm that requires an operation to be done multiple times, it may be appropriate for a function to call itself. Once more, do a board search, and you'll find many examples of how recursion is used.

edit: cflorinel - please refer to the announcement at the top of the forum, and learn how to use code tags.

5. How can you not know how that works? It tells you how it works when it runs.

Quzah.

6. Originally Posted by Kleid-0
I'll just explain recursion.
Code:
```void blah(void)
{
int n = 2+2;
// THIS is recursion:
blah();
}```
It is quite important for recursion to have an ending condition.
Above code will loop infinetly.
Second problem with this modification
instead of k = k + summing(1);
put k = k + summing(0);
or k = 1 + summing(1);
Will recursively compute 1+2+....+n = (n(n+1))/2

7. Originally Posted by cflorinel
It is quite important for recursion to have an ending condition.
Yes.

Originally Posted by cflorinel
Above code will loop infinetly.
No. theoretically, yes. In reality, no. You'll run out of stack space and the program will crash.

Quzah.

8. for: quzah and sean_mackrory.
Errors within the script were corrected for the first problem in my post.
I can see you are for quite some time in this forum and you can't even modify 2 lines to make it work. I suppose aulmedic555 is a begginer and he is looking for help, not for critics about code.
PS: I modified my code with CODE tag - I'm not familar with vBulletin.

9. I suppose aulmedic555 is a begginer and he is looking for help, not for critics about code.
We have indeed been on the forum for a long time. That's why we recognize the signs of someone looking for help when they haven't put in the effort themselves.

10. Just shut the I am sillyI am sillyI am sillyI am silly up. See what happens is people grab some code off of Google or some place, and without even trying to figure out what it does, they stick it up here, have people tell them what it does, so they can turn it in for homework or something. Then some idiot like you comes along and does all the work for them.

It's OBVIOUS they didn't write the code. It's also obvious that they didn't even try to figure it out. Hell, all they had to do is compile it and run it, and it would have TOLD THEM what it did.

It's also obvious that YOU didn't read the forum announcements before posting. If you had, you wouldn't have needed to edit your post, because you would have been shown how to use code tags. So don't come in here and try to start talking big, or you'll get a beating.

For the record, most people who just show up and post some random code and say "How does this work?" are NOT looking for help. They're looking for someone to do everything for them, just like you did.

Quzah.

11. You guys we've probably had this arguement before like 5000e^71283 times! Let's just let the river flow instead of building dams that keep breaking.

12. I think it's frustration - about 80% of the stuff on this forum is from under-motivated types looking for shortcuts.

People who don't read forum guidelines seem to also fall into this category more often than not.

13. Let them have free time while we figure out the problems. It's a win-win situation! We get stronger in programming, and they have fun doing something else, hehehe.

14. It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity. To try and stem the flow, so that those few who actually seek knowledge may make it to the shores of it safely, and not drown in the never ending sea of said ignorance. So that they might find a foothold or purchase to pull themselves with great effort to some beacon of hope, some refuge, and not be dragged down into the swirling abyss of stupidity, clawed at by the masses who would take them down with them. So I valiantly set the right from the wrong, a beacon of light in the dark chasm of inanity...

Quzah.

15. It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity. To try and stem the flow, so that those few who actually seek knowledge may make it to the shores of it safely, and not drown in the never ending sea of said ignorance. So that they might find a foothold or purchase to pull themselves with great effort to some beacon of hope, some refuge, and not be dragged down into the swirling abyss of stupidity, clawed at by the masses who would take them down with them. So I valiantly set the right from the wrong, a beacon of light in the dark chasm of inanity...
Excuse my ignorance but did you just come up with that or is that a quote I don't recognize?