Skip to content

Instantly share code, notes, and snippets.

@itskingori
Created December 20, 2013 07:00
Show Gist options
  • Save itskingori/8051335 to your computer and use it in GitHub Desktop.
Save itskingori/8051335 to your computer and use it in GitHub Desktop.
Function With Return Values
<!DOCTYPE html>
<html>
<head>
<title>Exercise 10: Functions with return values</title>
</head>
<body>
<?php
// Create 3 functions that do the following ...
// a) Calculate circumference of a circle ($radius as argument) – call the function ‘circle_circumference’
// b) Calculate the area of a circle ($radius as argument) – call the function ‘circle_area’
// c) Calculate the volume of a cylinder ($radius, $height as arguments) – call the function ‘cylinder_volume’
// Each much have a return and not output the result directly. Call the
// functions with $radius = 21 and $height = 20 where relevant and output
// the result.
// Create constant of PI (π)
define("PI", 22/7);
// Create function that calulates circumference of a circle (π x D)
function circle_circumference($radius) {
$circumference = PI * ($radius * 2);
return $circumference;
};
// Create function that calculates area of a circle (π x R x R)
function circle_area($radius) {
$area = PI * ($radius * $radius);
return $area;
};
// Create function that calculates volume of a cylinder (π x R x R x H)
function circle_volume($radius, $height) {
$volume = circle_area($radius) * $height;
return $volume;
};
// Output the calulation result
echo "The circumference is ".circle_circumference(21)."<br/>";
echo "The area is ".circle_area(21)."<br/>";
echo "The volume is ".circle_volume(21, 20);
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment