# recursive function question

This is a discussion on recursive function question within the C Programming forums, part of the General Programming Boards category; Okay, so I've got this recursive function. Here's the prototype: Code: void someFunction( const int b[], int startIndex, int size ...

1. ## recursive function question

Okay, so I've got this recursive function.
Here's the prototype:
Code:
`void someFunction( const int b[], int startIndex, int size );`
It takes as arguments an int array, a counter for the subscript (it's intiialized to 0 in main) and size, which is #DEFINE'd at 10 (the length of array b).

Here's the function:

Code:
```void someFunction( const int b[], int startIndex, int size ) {

if(startIndex < size) {
someFunction(b, startIndex+1, size);
printf("%d ", b[startIndex]);
}
} /* someFunction */```
Okay. So I get recursive functions, and I know what the function does, it counts up through startIndex to get to the end of the array, then counts back down and outputs the array's values in reverse order. I get the count up, but I'm having trouble understanding why/how the countdown works.
Thanks,
Colin

2. Picture a stack of blocks or something (the word "stack" is actually the technical term here, when one function calls another function which calls another function, etc.... it's called a "call stack"). There's two parts to the function - the first part calls a function on the next element. That's kind of like adding a new block to the stack. That new block, now has IT'S two parts to execute, and the first thing it does is add another tile to the stack.

Once there are no more blocks (i.e. the count "up" has finished), the block on top of the stack then executes it's second part, which is to print some data and return. When it returns, it's removed from the stack, and the next block down can execute IT'S second part. And so as each block executes the second part, one by one the blocks are removed.

I hope that visual helps...

3. Inside your IF block, someFunction() is called, therefore delaying the printf() below it. The last time in the "upward direction", the IF fails, because startIndex is finally 10, so the function exits, doing nothing After it exits, it returns back up the call stack to the IF block for startIndex 9, and then continues with the next instruction after the call to someFunction(), which is the printf(). When it exists, it goes back in the call stack where startIndex is 8, does the printf(), and exits, etc., etc., etc.

4. Ah, you beat me to it Sean.

5. There is no counting down.

As each recursive function ends, you get the old value (before you added one to it).

6. Oh! So the repeated calls to the function are still waiting to finish because the if blocks haven't finished executing. It gets as far as counting up 0 through 9, but since all those previous function calls haven't finished, they still have to do their thing, they're just waiting for the looping function calls to get done. The most recent one to execute finishes first, and then they just go down the list. Is that it?
Thinking of it as a stack helps a lot. I get how those work (FILO). It's amazing the simple things these books could say that would be so helpful that they leave out.

7. You got it!

8. It's amazing the simple things these books could say that would be so helpful that they leave out.
That happens a lot. When I'm reading about something, I'll try read it from 3 different sources - they all seem to explain things differently, and one of them will have a writing style that just helps a certain concept click.

9. cosmic_cow - you made the list.

10. Ha! That's awesome.

11. It's a gift. Cherish it.