Jump to content

Function not returning value?


Spreegem

Recommended Posts

So I'm working on a divide and conquer function to find the largest value in an array but I don't seem to get any value returned, when I echo theres nothing. Any ideas? Thanks a bunch in advance.

 

 

<?php
$array_size = 9;
$numbers = array(8, 6, 18, 14, 24, 30, 12, 26, 16);

//print_r($numbers);

function largest($x, $y)
{
    if ($x > $y)
    {
    	echo">y<br>";
        return $x;
    }
    else
    {
    	echo"!>y<br>";
        return $y;
    }
}

function maxArray($array_size, $first, $last)
{
    if ($array_size == 1)
    {
    	echo"=1<br>";
        return largest($numbers[$first], $numbers[$last]);
    }
    else if($array_size > 1)
    {
    	echo">1<br>";
        return largest(maxArray((int)($array_size/2), $first, (int)($first + $last)/2), maxArray((int)($array_size/2), (int)($first + $last)/2, $last));
    }
}

$max_value = maxArray($array_size, 0, ($array_size - 1));

echo "The largest value in the array is: $max_value";
?>

Link to comment
https://forums.phpfreaks.com/topic/178488-function-not-returning-value/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.