-1

I have the following problem: The recursive counterFunction returns not the last value but instead it seems to count down my $counter variable and then returns in every case 1. Unfortunately it also doesn't work when I put the return in both the if or else.

I highly appreciate every answer on this. Thanks in advance.

$counterReceive = counterFunction(0);
echo "CounterReceive: ".$counterReceive."</br>";

function counterFunction($counter)
{
    if ($counter < 3) {
        $counter++;
        echo "counter in recursive loop: ".$counter."</br>";
        counterFunction($counter);
    }
    echo "end condition reached.</br>";
    echo "End Counter: ".$counter."</br>";
    return $counter;
}

Output:

counter in recursive loop: 1  
counter in recursive loop: 2  
counter in recursive loop: 3  
end condition reached.  
End Counter: 3  
end condition reached.  
End Counter: 2  
end condition reached.  
End Counter: 1  
CounterReceive: 1  

Photo of my Output that describes the problem in a nutshell

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • 1
    Proper indentation of your code makes it much more readable... – Devon Bessemer Jul 23 '18 at 13:07
  • Imagine that the inner call to `counterFunction($counter)` is calling any other function, because it really doesn't matter that you're calling the function from within itself. That would explain the behaviour perfectly, no? – deceze Jul 23 '18 at 13:09
  • 1
    what is the issue ? everything works fine as the posted out put ? – LearningToCode Jul 23 '18 at 13:11
  • Your issue is not an issue. Can you please explain what do you expect your output to be like? – Alex Jul 23 '18 at 13:13
  • @devon: Thanks for your hint! – Lorenz Minks Jul 23 '18 at 13:14
  • @alex: The return should be 10 and so the $counterReceive. So the second half of the output should not be there. – Lorenz Minks Jul 23 '18 at 13:15
  • so you want `$counterReceive = counterFunction(0); echo $counterReceive;` to output 10 ? – Alex Jul 23 '18 at 13:17
  • @alex that's right. The Code I'm working on is more complex, but this shows the core problem without the other stuff that's not needed for this problem – Lorenz Minks Jul 23 '18 at 13:18
  • There is no "core problem". The problem is the way you describe your goal. We don't need 10 loops to understand the problem. You can always use just 3 if you want something to simplify. And you don't need to post your debugging useless output, but just raw data, current output and desired output - those are way more useful – Alex Jul 23 '18 at 13:23

3 Answers3

1

You don't save the returned value.

Change line

counterFunction($counter);

to

$counter=counterFunction($counter);
jcjr
  • 1,503
  • 24
  • 40
1

It does exactly as you tell it to.

function counterFunction(int $counter) :int
{
    if ($counter < 3) {
        $counter++;                                            // mutate $counter to be increase by 1
        echo "counter in recursive loop: ".$counter."</br>";   // print info with new $counter binding
        counterFunction($counter);                             // call counterFunction AND DISCARD the result (it is not assigned or returned)
    }
    // This happens for each call regardless if it does recursion or not
    echo "end condition reached.</br>";                        // print info
    echo "End Counter: ".$counter."</br>";                     // print End counter for every stage or recursion, which is argument increased if argument was below 3
    return $counter;                                           // return argument increased by 1 regardless of possible recursion
}

You may think that two calls of counterFunction shares $counter, but they don't. They have their own copies of the argument unless it is an object or passed by reference. In this case it isn't.

To mend it you should use the result from the recursion. Eg.

function counterFunction(int $counter) :int
{
    if ($counter < 3) {
        echo "counter in recursive loop: {$counter}</br>";     // print info with current $counter binding (argument)
        return counterFunction($counter+1);                    // call counterFunction with $counter+1 as argument and returne the result
    }
    // only happens when $counter >= 3. returns argument
    echo "end condition reached.</br>";                        // print info
    echo "End Counter: {$counter}</br>";                       // print End counter, which is the same as argument
    return $counter;                                           // return argument
}

Or you can update your counter with the result:

function counterFunction(int $counter) :int
{
    if ($counter < 3) {
        echo "counter in recursive loop: {$counter}</br>";     // print info with current $counter binding (argument)
        $counter = counterFunction($counter+1);                // call counterFunction with $counter+1 as argument and update $counter
    }
    // This happens for each call regardless if it does recursion or not
    echo "end condition reached.</br>";                        // print info
    echo "End Counter: {$counter}</br>";                       // print End counter, which is the same as argument
    return $counter;                                           // return argument
}

This has the possibly unwanted effect that you'll have several printings with "end condition" than the actual base case since every call with do it.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
0

I have no idea what is your final goal. To me this function seem useless at the moment.

Here is my approach to return 10 :-)

https://ideone.com/jorZID

function counterFunction($counter){
  if($counter < 10){
    $counter++;
    echo "counter in recursive loop: ".$counter."</br>";
    return counterFunction($counter);
  } else {
    return $counter;
  }
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
Alex
  • 16,739
  • 1
  • 28
  • 51