0

I am practicing scala's simple recursive function. This is from a book

def calculatePower(x:Int, y:Int): Long = {

     if (x>=1)
         x*calculatePower(x,y-1)

     else 1

  }


calculatePower(2,2)
1
  • you're checking x but reducing y Commented Apr 5, 2017 at 17:21

2 Answers 2

2

You are checking x but you are decrementing y. That means your base-case will never be reached.

Sign up to request clarification or add additional context in comments.

Comments

0

Your method stack overflows because it doesn't terminate and the stack frames accumulates until there is no more room.

if (x>=1) x*calculatePower(x,y-1) You test if x is greater or equal to 1 but in the recursive call you only decrement y!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.