RETURNING VALUES from User-Defined Functions

Chapter 7 #136 - Martha's CIS 52

ASSIGNMENT/EXPLANATION

"A function can return a value using the return statement in conjuction with a value. The return statement stops the execution of the function and sends the value back to the calling code." (136) The return statement can be hard-coded (4), the result of an expression ($a/$b), or the value returned by another function call (another_function($an_argument)). In this example, the addNums() function is called with two numeric arguments that are stored in the variables $firstnum and $secondnum. The function adds the two numbers, and stores the result in a variable called $result. The returned $result variable is printed to the browser page when the addNums() function is called.

Exercise

8

CODE

<?php 
function addNums($firstnum, $secondnum) {
$result = $firstnum + $secondnum;
return $result;
}
echo addNums(3,5);
?>