# query in reccursion

Discussion in 'C Programming' started by bnslpayal@gmail.com, Dec 11, 2007.

1. ### Guest

int giValue = 5; /* Global Variable Declaration */
void fnReverse()
{
if (giValue > 0) {
giValue--;
fnReverse();
}
printf("%d\n",giValue);
}

/
************************************************************************************************************
* why the answer is 0 six times????????????????

**********************************************************************************************************/

, Dec 11, 2007

2. ### Eric SosmanGuest

wrote:
> int giValue = 5; /* Global Variable Declaration */
> void fnReverse()
> {
> if (giValue > 0) {
> giValue--;
> fnReverse();
> }
> printf("%d\n",giValue);
> }
>
> /
> ************************************************************************************************************
> * why the answer is 0 six times????????????????
>
> **********************************************************************************************************/

"Six times" because the values 5,4,3,2,1,0 are a set
of cardinality six.

"0" because giValue is equal to zero at each of the
six executions of the printf call.

--
Eric Sosman
lid

Eric Sosman, Dec 11, 2007

3. ### Golden California GirlsGuest

wrote:
> int giValue = 5; /* Global Variable Declaration */
> void fnReverse()
> {
> if (giValue > 0) {
> giValue--;
> fnReverse();
> }
> printf("%d\n",giValue);
> }
>
> /
> ************************************************************************************************************
> * why the answer is 0 six times????????????????
>
> **********************************************************************************************************/

Because you hit the printf statement 6 times for this loop. Consider this, if
giValue was initialized to zero you would still print it's value once.

Golden California Girls, Dec 11, 2007
4. ### Chris DollinGuest

wrote:

> int giValue = 5; /* Global Variable Declaration */
> void fnReverse()
> {
> if (giValue > 0) {
> giValue--;
> fnReverse();
> }
> printf("%d\n",giValue);
> }
>
> /
> ************************************************************************************************************
> * why the answer is 0 six times????????????????
>
> **********************************************************************************************************/

Because you print the value of `giValue` after the recursion has been
all the way down to the bottom.

As would have been obvious if you'd tried "play machine" or attempted the

--
Chris "once a formalist, always (but not only) a formalist" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Chris Dollin, Dec 11, 2007