Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the workflow of Pow(x,y) function?

I'm going through "sololearn" and udemy courses to try to learn C#. I am doing the challenges but could not figure out how the below code resulted with 32 (as in 32 is the correct answer here and I am trying to find out why). Can someone explain this process to me, the method calling itself is throwing me I think.

static double Pow(double x, int y)
{
    if (y == 0)
    {
        return 1.0;
    }
    else
    {
        return x * Pow(x, y - 1);
    }
}

static void Main()
{
    Console.Write(Pow(2, 5));
} 

Please excuse my bad coding. I am trying to do it on mobile, which is difficult, the answer was 32. Can someone explain why?

Edit: Aplogies here is how I work through it. Pass 2 and 5 to Pow, check if y == 0 which is false, it is now y == 5 so x * pow(x, y-1) formula will be active. X is still 2, y is now 4 which means it fails the check again on whether it equals 0, this loop continues until it returns the 1.0, x has remained at 2 so 2 * 1.0 = 2 and not 32?

like image 837
Fuss Avatar asked Nov 27 '22 16:11

Fuss


2 Answers

First thing to note is that this is NOT how you would normally implement a power function. It's done this way to demonstrate recursion.

With that out the way, let's look at what happens when you call Pow(2, 5):

Pow(x = 2, y = 5)
    -> return 2 * Pow(2, 4)
<- 2 * 16 = 32

  Pow(x = 2, y = 4)
      -> return 2 * Pow(2, 3)
  <- 2 * 8 = 16

    Pow(x = 2, y = 3)
        -> return 2 * Pow(2, 2)
    <- 2 * 4 = 8

      Pow(x = 2, y = 2)
          -> return 2 * Pow(2, 1)
      <- 2 * 2 = 4

        Pow(x = 2, y = 1)
            -> return 2 * Pow(2, 0)
        <- 2 * 1 = 2

          Pow(x = 2, y = 0)
              -> return 1 (because y == 0)
          <- 1

To read this representation of the recursive call stack, work your way from the top to the bottom looking at how the arguments change; then work your way back up from the bottom to the top looking at the return values (which I indicate with <-).

like image 62
Matthew Watson Avatar answered Dec 11 '22 20:12

Matthew Watson


Ok so let's go through the whole thing.

First of all, a static function is one that can be called without need to instantiate an object. There is one signature that all objects of the same class share. The double is a type within C# and its appearing here to show what the final output type of the function will be. Pow is the name of the function, double x, int y are parameters described by their type (not very well named but we'll leave that for another day)

So x is a number and y is the power of that number. There is a conditional here to check for two outcomes. If y is 0 then the answer is always 1, simple maths. Otherwise, the function performs the arithmetic using recursion (it calls itself again until it meets a terminating condition). The reason we get 32 is because 2x2x2x2x2 = 32. It is 2 to the power of 5.

I'm presuming you know what main and console.write are.

like image 43
Xerith Avatar answered Dec 11 '22 20:12

Xerith