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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:02:56+00:00 2026-06-14T23:02:56+00:00

today 1st time i’m trying to learn recursive function thru online tutorials but stuck

  • 0

today 1st time i’m trying to learn recursive function thru online tutorials but stuck at beginning stage. i found below code but its provide me ‘output: 1’ against any value.
so, i need better explanation of that:

function factorial($n){
if($n==0){
return 1;
}
return fact($n, 1);
}
function fact($i, $j){
if($i<1){
return 1;}
else {
return fact($i-1, $i*$j);
}
}
echo factorial(5);

one more thing, i need clarification of how below return method:

return fact($i-1, $i*$j);

will work to convey single value from two parameters. any1 plsss give me some ideas regarding this issue to clear my concept. Thnx in advance..

  • 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-14T23:02:57+00:00Added an answer on June 14, 2026 at 11:02 pm

    You have a function factorial here that calls a recursive function fact. A recursive function always works like this:

    • If you call it with a “trivial” argument, it can immediately give you the answer. In your code, that is the part saying if($i<1){ return $j; } (As per the comment of @Sreenath)

    • If the argument is more “complicated”, the function simplifies the argument (that is $i-1 in your example: The trivial case is $i<1, so making $i smaller makes the argument easier in some way) and then calls itself with the simpler argument and possibly some additional information, which is where the fact($i-1, $i*$j) call comes from.

    So the recursive function fact here works doing the following thing:

    fact(i, j) = fact(i-1, i*j)
     = fact(i-2, (i-1)*(i*j)) = fact(i-3, (i-2)*(i-1)*i*j)
     = ... = fact(1, (i-(i-1)) * (i-(i-2)) * ... * (i-1) * i * j)
     = fact(0, (i-i) * (i-(i-1)) * (i-(i-2)) * ... * (i-1) * i * j)
     = (i-i) * (i-(i-1)) * (i-(i-2)) * ... * (i-1) * i * j # Because 0<1
     = i! * j
    

    Now if you want just the factorial, you need to call fact with 1 as second argument, just as you do in return fact($n, 1);.

    function factorial($n){
      if($n==0){ # The trivial case
        return 1;
      }
      # Every other case is "complicated": call a specialized function.
      return fact($n, 1);
    }
    
    function fact($i, $j){
      # Helper function: returns i!*j, doing a recursive calculation.
      if($i<1){ # The trivial case
        return j;  # i!*j for i<1 is just j
      }
      else { # The "complicated" case:
        return fact(
            $i-1,  # Simplify the argument
            $i*$j  # Pass my current state down
          ); # And call myself with the simpler argument and the internal state.
      }
    }
    
    # Test it: This should return 5!=120
    echo factorial(5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using Xcode 3.x but today (nov 1st 2011) have downloaded Xcode 4.2
today I was trying to use the SnowballAnalyzer on Lucene Java API v3.6.0 but
Consider the following 2 scenarios: Scenario 1). Today is May 1st 2012, and Scenario
I am trying to calculate, based on today's date (24 August 2012), the following
How do i get today's date, 1st day of current month and last day
Today I started learning libxml for the first time. And kept struggling to find
How to check weather today is the 1st day of the year or not
1st of all, sorry for using that channel, but there's no other open communication
today i hit a really annoing problem with wpf. i just want to align
Today at work, I was given a task to fix a bug within our

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.