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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:21:42+00:00 2026-06-11T21:21:42+00:00

Please help me understand this simple recursion. I’m very new to programming, so I’m

  • 0

Please help me understand this simple recursion. I’m very new to programming, so I’m wondering how exactly this happens step by step.

I’m trying to learn recursion, but I’m stuck with the addition of “+”

function foo($bar) {
  if ($bar == 1) return 1;
  elseif ($bar == 0) return 0;
  else return foo($bar - 1) + foo($bar - 2);
}
  • 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-11T21:21:43+00:00Added an answer on June 11, 2026 at 9:21 pm

    When trying to understand recursion I find it helps a lot to write down each individual case for a specific parameter and then build your understanding from there.

    So let’s take the example of foo(3)

    foo(3) -> we don’t hit either base case, so our function now wants to return

    foo(2) + foo(1)
    

    First we need to get foo(2)

    foo(2) -> Again no base case, so we return

    foo(1) + foo(0)
    

    foo(1) = 1 and foo(0) = 0 (these are our base cases) so we see that

    foo(2) = 1 + 0
    

    Now our look at foo(3) is resolved to

    foo(3) -> (1 + 0) + foo(1)

    foo(1) = 1, so we can finally see that

    foo(3) -> (1 + 0) + 1 = 2

    You have to remember that recursion is basically building a “tree” of function calls – it’s going to go as far down the tree as possible, and then come up one level and see what else it has to do to continue. I’m not sure how clear this is but hopefully it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me understand this. I created a really simple program to try to
Please help me understand this recursive function... var stack = Array; function power(base, exponent){
I'm a noob; please help me understand this authentication config / bindings stuff that
Please help me. I can't understand why this function, that uses texture memory __global__
Please help me with this update query. I COMPLETELY understand how redundant this is
I am missing something simple here, please help me understand what. My Controller sets
Please help me understand why this isn't working. I don't know if there is
I am new to MVC and dependency injection. Please help me to understand how
Please help me understand the following code snippet :- def any(l): whether any number
Please help me understand why add1() and add4() report errors and why add2() 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.