Velocity Reviews > query in reccursion

# query in reccursion

bnslpayal@gmail.com
Guest
Posts: n/a

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

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

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

Eric Sosman
Guest
Posts: n/a

 12-11-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
(E-Mail Removed)lid

Golden California Girls
Guest
Posts: n/a

 12-11-2007
(E-Mail Removed) 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.

Chris Dollin
Guest
Posts: n/a

 12-11-2007
(E-Mail Removed) 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