# Confused

• 05-29-2006
jeev2005
Confused
Hi,

Below i have pasted the code, now i understand that this is a recursive algorithm however when i step through this im really confused .

I understand that the Scale (5) line states the starting number for the value of 'n' once this is found the program then enters the first if statement, checks to see if its greater than 1 if it is it then minuses 1 from n's value and repeats this until n is not greater than 1 or is equal to 1.

From that point it then moves forward to the if n ==1 line where it prints a single dash and line space. After that it then enters the FOR loop. - This is where i get confused

the counter within the FOR loop is set to 0 if the counter is less than the value of N it then increments the counter by 1 and again prints a single dash followed by a line space.

it then steps onto the scale (n - 1) line this then minuses 1 from the value of n making it zero.

N = 0 at this point the program returns to the first if statement to check it n > 1 which is isnt and moves into the n == 1 statement and prints a single dashed line.

the counter and n are both equal to zero this stage are they or arent they? shouldnt the program stop there?

i know the actual outcome but cant get my head round why its like that?

Code:

```void scale( int n ) { int counter; if ( n > 1 ) { scale( n - 1 ); for ( counter = 0; counter < n; counter++ ) printf( "-" ); printf( "\n" ); scale( n - 1 ); } else { if ( n == 1 ) printf( "-\n" ); } } int main( void ) { printf( "\n" ); scale( 5 ); }```

Quote:

-
--
-
---
-
--
-
----
-
--
-
---
-
--
-
-----
-
--
-
---
-
--
-
----
-
--
-
---
-
--
-
• 05-29-2006
King Mir
You should draw everything out on a peice of paper to figure out what's going on.

Remmember that every call to scale() has it's own 'n'.

I'll take you through the first few calls to scale():

1)scale(5) is called. Becouse 5>1, scale(5-1) is called.
2)scale(4) is called. As before scale(4-1) is called.
. . .
5)scale(1) is finally called. This prints "-\n" and returns to scale(2)
6)scale(2) gose through the for loop, printing two dashes.
7)scale(2) calls scale(1). This prints "-\n" and returns to scale(2)
8)scale(2) is finished. it returns to scale(3)
9)scale(3) gose through the for loop, printing three dashes.
. . .
• 05-29-2006
jeev2005
thats where i was getting confused ok so once it fulfils the criteria it returns to 2...... why is this is this just the way the recursion works?

Really Appreciate you response!

Thanks
• 05-29-2006
Happy_Reaper
Well scale(2) is waiting for scale(1) to complete, and it can only proceed once it is completed. So once scale(1) completes, it returns to scale(2) which can then proceed. After scale(2) would complete, scale(3), which had been waiting for scale(2) to complete in order to proceed, can continue, and so on...

Make any sense ?

And yes, without going into the beefy details, that is how recursion works.
• 05-29-2006
jeev2005
Yes kind of , i will post tomorrow as i must rest now. Thanks for you help..... if i struggle to understand this any further i will post again...... :)
• 06-01-2006
King Mir
Honestly, I'm not kidding, you should draw it out on paper.

When a function makes a call to another function, it stops and waits for that function to return. Then it continues where it left off, replaning the function call with the returned value or void.