Reputation: 194
I'm a beginner to php and I've had difficulty understanding what returning a value means in php. So far, I understand it as this: once you return a value, that's the final value that the function will hold and all scripts that happens after you return a value in a function (and before the function ends) will be terminated.
The first script that I'm printing is what I see from a book. I thought we can just rewrite the first script as the second script, and I don't see any differences between the script.
Am I wrong about either my understanding of returning a value and my idea of the following two scripts being the exact same? I tried to read as much as I can about returning values and nothing seemed to explain it clearly enough to me. I want to really understand exactly what's the point of having to return a value and why we don't just use Script #2 instead of #1. I'd appreciate your help. Thank you.
Script 1:
function addNums($firstnum, $secondnum){
$result= $firstnum + $secondnum;
return $result;
}
echo addNums(3,5);
Script 2:
function addNums($firstnum, $secondnum){
return $result= $firstnum + $secondnum;
}
echo addNums(3,5);
Upvotes: 1
Views: 137
Reputation: 23062
I would suggest start your coding journey with a best practice:
Be explicit about precedence.
In other words, use parentheses liberally. They may be beneficial in clarifying the order of operations you want, and they won't hurt.
Instead of:
return $result= $firstnum + $secondnum;
You might want to use:
return ($firstnum + $secondnum); // The $result = is just unnecessary complication
, using parentheses when entering a complex expression on your return line. This is not actually necessary in the case you have here, but could help you avoid problems in the future.
Here's a better case where you definitely want to lean on parentheses to clarify your math:
return (($firstnum + $secondnum) / (4 - $thirdnum)); // or more complicated
So essentially, the more complex your return statements get, the more parentheses become your friend (until you have the operator orders completely down).
But other than that, putting expressions in your return line is all fine and good. The one problem that you will run into while doing so is that you won't be able to debug the final returned value as easily as if you use a result variable:
var_dump($result); // Can't echo out/var_dump the variable unless you've a temp variable to output
return $result;
But on the other hand, that is what Test Driven Design and writing your tests to ensure you get the results you're expecting is for.
Upvotes: 3
Reputation: 95101
I think i prefer this
function addNums() {
return array_sum(func_get_args());
}
Or Better Still using bc_maths
function addNums() {
return array_reduce(func_get_args(), function ($a, $b) {
return bcadd($a, $b);
});
}
Upvotes: 3
Reputation: 2491
The second script should be along the lines of:
function addNums($firstnum, $secondnum){
return ($firstnum + $secondnum);
}
echo addNums(3,5);
Upvotes: 0
Reputation: 57312
better approach would be
function addNums($firstnum, $secondnum){
return $firstnum + $secondnum;
}
Upvotes: 5