Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:37:07+00:00 2026-06-14T15:37:07+00:00

Below recursive method sums the integer values between a range def sumInts(a: Int, b:

  • 0

Below recursive method sums the integer values between a range

  def sumInts(a: Int, b: Int): Int = {
    if(a > b) 0
    else {
      println(a +"," + b)
      a + sumInts(a + 1 , b)
    }
   } 

So sumInts(2 , 5) returns 14

I’m confused about how the recursive call to sumInts sums the integer range. Can explain textually how this method works ?

How does sumInts return the incremented value ?? Perhaps I am missing something fundamental to recursion here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:37:09+00:00Added an answer on June 14, 2026 at 3:37 pm

    It calculates the sum of values in the range [a, b] by first calculating the sum of the range [a+1, b] (by recursively calling sumInts(a + 1 , b)) then adding a to it.

    [Update] In Scala, the return statement is optional; functions return the value of the last expression evaluated. Thus the above function body is equivalent to

    if(a > b) return 0
    else {
      println(a +"," + b)
      return a + sumInts(a + 1 , b)
    }
    

    [/Update]

    Which for the range [2, 5] it would do the following (I removed the println call for the sake of simplicity, and added brackets to mark recursive calls):

    1. if(2 > 5) 0 else 2 + sumInts(2 + 1, 5) which, the condition being false, evaluates to
    2. 2 + sumInts(3, 5)
    3. 2 + (if(3 > 5) 0 else 3 + sumInts(3 + 1, 5)) which evaluates to
    4. 2 + (3 + sumInts(4, 5))
    5. 2 + (3 + (if(4 > 5) 0 else 4 + sumInts(4 + 1, 5))) which evaluates to
    6. 2 + (3 + (4 + sumInts(5, 5)))
    7. 2 + (3 + (4 + (if(5 > 5) 0 else 5 + sumInts(5 + 1, 5)))) which evaluates to
    8. 2 + (3 + (4 + (5 + sumInts(6, 5))))
    9. 2 + (3 + (4 + (5 + (if(6 > 5) 0 else 6 + sumInts(6 + 1, 5))))) which, the condition being true, evaluates to
    10. 2 + (3 + (4 + (5 + (0))))
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

below is a class method that always returns null -when, for example, stack =
For example I make the recursive call defined below. The method finds the kth
I have a recursive method which is simplified below: private List<string> data; public string
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
I'm writing a recursive method that basically traverses a NSManagedObject object and converts it
I'm trying to create a bone and IK system. Below is the method that
A method, printStars(j) , is available that returns a string -- a row of
Ok so, I have a read method that is reading the values in correctly
I have a recursive method that reversed a string (HW assignment, has to be
Possible Duplicate: default method argument with class property? I'm writing a recursive function and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.