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 8117549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:12:36+00:00 2026-06-06T04:12:36+00:00

I do not see how this recursive function works: function f(n) { g(n-1) }

  • 0

I do not see how this recursive function works:

function f(n) { g(n-1) }

function g(n) {
   alert("before: " + n);

   if(n > 0) f(n);

   alert("after: " + n);
}

f(2)​;​

I have tried to understand this code works and I saw how “before 1”, “before 0”, and “after 0” execute but… how does “after 1” come from this?

I see it executing as this… f(2) calls g which subtracts 1 so ‘n’ becomes 1. Alert(“before: ” + n) is executed, 1 is greater than 0 so it will recall itself and subtract 1. Alert(“before:” + n) is executed once again, 0 is not greater than 0 so it will execute Alert(“after:” + n) and the function ends?…

Edit: Thank @FlorianMargaine and @Cyrille for helping me understand the logic behind this. =)

  • 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-06T04:12:38+00:00Added an answer on June 6, 2026 at 4:12 am

    after 1 comes from the fact that in Javascript, parameters are passed by value instead of by reference. So on the first iteration, calling if(n > 0) f(n); which calls g(n-1) does NOT decrement n. Its value is kept for when if(n > 0) f(n) returns, and remains at its value of 1.

    Here’s a call graph:

    f(2) calls f(n) with n=2
    ├─ calls g(n-1), which is g(2-1) = g(1)
    │   ├─ g(1) alerts "before: 1"
    │   ├─ g(1), n > 0 ==> call f(1)
    │   │   ├─ f(1) calls f(n) with n=1, but it's not the same "n"
    │   │   │  as we're in another call (see the call tree?)
    │   │   │   ├─ calls g(n-1), which is g(1-1) = g(0)
    │   │   │   │   ├─ g(0) alerts "before: 0"
    │   │   │   │   ├─ n == 0 ==> don't call f(n)
    │   │   │   │   └─ g(0) alerts "after: 0"
    │   │   └─ and that's all for f(1)
    │   └─ g(1) alerts "after: 1"
    └─ and that's all
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why do I not see the spinner image when I run this code? However
I see that this has been asked many times. But, unfortunately I have not
Can anybody see what is wrong with this code. it does not show up
I see this question here on SO but I am not sure that answers
As(no pun intended) you can see in this screenshot the AS statement is not
I'm not sure if this is possible but I want to see objects created
Does anyone see a problem with this, its not working saying bad file descriptor
Consider this example: zh_Hant_HK format = yy'年'M'月'd'日' ah:mm Not sure if you can see
I have a function that I want to execute several times for different DOM
I was sent this nice non-recursive function for computing a fibonacci sequence. So I

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.