Thread: Doub

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Doub


    <?php
    function factorial_of_a_number($n)
    {
    if($n ==0)
    {
    return 1;
    }
    else
    {
    return $n * factorial_of_a_number($n-1);
    }
    }
    print_r(factorial_of_a_number());
    ?>

    when i run this program it gives correct result..but i dint understand " return $n * factorial_of_a_number($n-1); "....this statement.....when i looked i felt like its giving a result 12...i know its wrong..but how is it possible??? can anyone help please??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    What are you doing? Just putting together snippets of code and then asking us why they do what they do? This is the second silly post you have put up.

    Instead of getting others to do your thinking for you, why don't you do your own research and teach yourself something?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    if i found what i needed..do you think i would ask you to help me??
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Please use more descriptive thread titles than "Doubt". That tells us nothing, and worse it makes us think you're creating multiple threads for the same question.

    That line is recursion: having a function call itself repeatedly. It works well for factorials because of how factorials work. As for returning 12, I don't know why because there's no number that has a factorial of 12.

IMN logo majestic logo threadwatch logo seochat tools logo