PHP: Returning a value from a function

This entry is part 33 of 54 in the series PHP Tutorial

To return value from functions, use the return keyword with a value to return.


function restaurant_check($meal, $tax, $tip) {
    $tax_amount = $meal * ($tax / 100);
    $tip_amount = $meal * ($tip / 100);
    $total_amount = $meal + $tax_amount + $tip_amount;

    return $total_amount;

$total = restaurant_check(25,7,15);

echo "Total spent is $total";

Javascript: Functions

This entry is part 13 of 27 in the series Javascript Tutorial

Functions are blocks of code that together achieve a more complex task, and are not executed until you call upon them.

We have already made use of functions in our earlier examples.  The example below will pass 2 variables into a function.

<!DOCTYPE html>
 function updateFunction(x, y) {
   var z = x + y;

   document.getElementById("int1").innerHTML = x;
   document.getElementById("int2").innerHTML = y;
   document.getElementById("int3").innerHTML = z;
 <p id="int1">Integer x</p>
 <p id="int2">Integer y</p>
 <p id="int3">Integer z</p>
 <button onclick="updateFunction(8,10)">Click Me</button>


The x, y variables are passed into the updateFunction function.  Try changing 8, 10 into different integers and run the above program again.