-2
\$\begingroup\$

In this program, I understood everything except power(x1,y1-1). I want to know how the power(x1,y1-1) function exactly works.

 #include<iostream.h>
 #include<conio.h>

 int power(int,int);

 void main()
 {
   int x,y,p;
   clrscr();
   cout<<"\n\n\t calculating power of a number using recursion";
   cout<<"\n\n\t Enter base ";
   cin>>x;
   cout<<"\n\n\t enter index";
   cin>>y;
   if(y==0)
   {
     cout<<"\n\n\tpower is 1";
   }
   if(y<0)
   {
     y=y*(-1);
     p=power(x,y);
     cout<<"\n\n\t power is"<<1/p;
   }
   p=power(x,y);
   cout<<"\n\n\t the power is"<<p;
   getch();
}

int power(int x1,int y1)
{
  if(y1==1)
    return(x1);
  else
    return(x1*power(x1,y1-1));
}
\$\endgroup\$

closed as off-topic by rolfl, Yuushi, ChrisW, syb0rg, Malachi Jan 19 '14 at 16:03

If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Please fix indentations of your program. It is very hard to read code without proper indentations. \$\endgroup\$ – bronislav Jan 19 '14 at 13:29
  • \$\begingroup\$ fixing indentation not mean add empty line between every line. I have fixed it for you. Next time you will know what it is mean by this phrase. \$\endgroup\$ – bronislav Jan 19 '14 at 13:47
  • 5
    \$\begingroup\$ This question appears to be off-topic because it is about code that is not your own. If you do not understand how the recursion works then you could not have written this code. \$\endgroup\$ – rolfl Jan 19 '14 at 13:47
  • \$\begingroup\$ Thanks. Actually i executed this code during practical in my college. But im really confused with this power function. \$\endgroup\$ – mac07 Jan 19 '14 at 13:53
  • \$\begingroup\$ There are plenty of things I did in college that I have forgotten. Don't ask me for examples, though: I forget what. \$\endgroup\$ – Wayne Conrad Jan 19 '14 at 14:43
2
\$\begingroup\$

The laws of recursion are:

  1. Change at least one argument while recurring. The arguments must be changed to be closer to termination
  2. The changing argument must be tested in the termination condition

(from The Little Schemer, a most excellent book about recursion)

The y1-1 in this line takes care of rule 1:

return(x1*power(x1,y1-1));

and this line takes care of rule 2:

if(y1==1)

Each time the function calls itself, it decrements y1. When y1 reaches 1, the function is done.

(You may also note that if y1 is less than 1, the function will not work correctly: mathematically, it will loop forever, but in actuality, on most architectures y1 will underflow, become positive, and eventually reach 1, at which point the function will return a wildly incorrect result).

It would be instructive to execute this function in your head--pretend to be the computer. Call the function with x1 = 2 and y1 = 3 and step through it, doing everything the computer would do. Use a pencil and a piece of paper to keep track of the values of x1 and y1, remembering that each recursive call creates a new stack frame with new, independent values of x1 and y2; each return destroys the most recent stack frame and restores x1 and x2 to the values they had in the previous stack frame.

\$\endgroup\$
  • 1
    \$\begingroup\$ " Use a pencil and a piece of paper to keep track of the values of x1 and y1, remembering that each recursive call creates a new stack frame with new, independent values of x1 and y2; each return destroys the most recent stack frame and restores x1 and x2 to the values they had in the previous stack frame." really worked .Thanks a lot !!!! \$\endgroup\$ – mac07 Jan 19 '14 at 14:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.