SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict dr_dumb99's Avatar
    Join Date
    Sep 2004
    Location
    Pakistan
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Recursive function behaviour

    I have got following code.
    PHP Code:
    $x 1;
    $y 3;

    function 
    func($x$y){

        if(
    $x <= 0){
            
            return 
    $y;
        }
        else{
            return 
    1+func($x-1,$y);

        }
    }

    echo 
    func($x$y); 
    Can some one explain that how and why it is showing " $x+$y " result.


    Thanks in advance

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Because... that's what your function does.

    You recurse down until X = 0. Return Y, and then for each level of X you recursed down, you add 1. Which, is in effect, x+y.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dr_dumb99 View Post
    Can some one explain that how and why it is showing " $x+$y " result.
    $x = 1, $y = 3
    ...
    return 1 + func(0, 3)
    = return 1 + 3



    $x = 2, $y = 4
    ...
    return 1 + func(1, 4) where func(1, 4) equals 1 + func(0, 4)
    therefore
    return 1 + (1 + func(0, 4))
    = 1 + 1 + 4
    = 6
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Addict dr_dumb99's Avatar
    Join Date
    Sep 2004
    Location
    Pakistan
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How $x+$y. It is creating confusion.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Do what computer programmers are taught from the beginning.
    Get out a pen and paper, draw 3 columns at the top. label 1 $x, 1 $y, and 1 ReturnValue.

    I have a feeling we're being asked to do someone's homework here.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dr_dumb99 View Post
    How $x+$y. It is creating confusion.
    Each time it drops $x by 1, it returns 1 + $y
    The more times it drop $x by 1, the more times it return 1 + $y

    Code:
    $x = 2, $y = 3
    func($x, $y) is a call to func(2, 3)
    When you call func(2, 3) the function calls func(1,3)
        When func(1, 3) is called, it calls func(0, 3)
            When func(0, 3) is called, it returns 3
        When func(1,3) gets the returned 3, it returns 1 + 3, which is 4
    When func(2,3) gets the returned 4, it returns 1 + 4, which is 5.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •