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

  • SEARCH
  • Home
  • 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 8695615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:04:58+00:00 2026-06-13T01:04:58+00:00

I am trying to understand the recursion call in the below code snippet. static

  • 0

I am trying to understand the recursion call in the below code snippet.

static long fib(int n) {
    return n <= 1 ? n : fib(n-1) + fib(n-2);
}

Which function call does get called first? How does the equation work after the calls?

Do both of these get called once and then equation applied or first one called and then the 2nd one?

Maybe a very simple question!

  • 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-13T01:04:59+00:00Added an answer on June 13, 2026 at 1:04 am

    Java and C♯

    Sub-expressions are evaluated in left-to-right order. fib(n-1) is evaluated before fib(n-2). See What are the rules for evaluation order in Java?

    It’s important to note that the order of evaluation doesn’t matter here since fib() does not have any side effects.

    C and C++

    The two functions are called in an indeterminate order, and once both have been called their return values are added together and returned. The left function could be called first, or the right one first, you don’t know.

    That may seem problematic, but it’s not, because the order they’re called doesn’t matter. Calling fib(i) does not have any side effects (e.g. modifying other variables, printing a message, etc.), so the two function calls are entirely independent.

    One compiler might decide to evaluate the left side before the right:

     1. f(3)
     2.   f(2)
     3.     f(1)
     4.       return 1
     5.     f(0)
     6.       return 0
     7.     return 1 + 0
     8.   f(1)
     9.     return 1
    10.  return 1 + 1
    

    Another one might decide to evaluate the right side before the left:

     1. f(3)
     2.   f(1)
     3.     return 1
     4.   f(2)
     5.     f(0)
     6.       return 0
     7.     f(1)
     8.       return 1
     9.     return 1 + 0
    10.  return 1 + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand the math of this code snippet. A token is provided which
I'm trying to understand within my lesson how to read the below function called
Trying to understand Ruby a bit better, I ran into this code surfing the
So I'm currently trying to grasp the concept of recursion, and I understand most
I'm really trying to wrap my brain around how recursion works and understand recursive
I am trying to understand tail-recursion in Haskell. I think I understand what it
I am trying to understand how the following code ties into two other .java
I'm trying to understand how recursion works in C. Can anyone give me an
I am trying to create and understand recursion in php/mysql my database looks something
I'm in the process of trying to understand recursion better, so I decided to

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.