| CS 199 | Willamette University | Spring, 2019 |
|
Programming in PHP, Databases with MySQL, and Web Applications |
||
The return statement is used for several purposes, including
to terminate a function,
to nest conditions within a function,
to process information derived from a function,
to catch errors. (If the error condition is met, the return statement is executed, and the function stops running.)
Examine the function below which compares two variables, and attempts to return the larger value:
function comparison ($x, $y){
if(!isset($x) || !isset($y)){
return 0;
} else if($x == $y){
return $x;
} elseif($x > $y){
return $x;
} else {
return $y;
}
}
In this function:
The parameters $x and $y are located inside the parenthesis.
The function isset is a built in function used to check if the variable exists with an assigned value.
If either $x or $y has not been assigned a value, the first return statement is executed. In this example, the function returns 0, so the result of the function can be used in arithmetic expressions, as shown below. Another possibility would be for the function to return the Boolean value false—a logical choice, but preventing the use of the function reliably in all arithmetical expressions.
The next line checks to see whether the two values are equal, and the subsequent lines check to see which value is larger. If the two values are equal, then either could be considered the larger. If first two returns are not triggered, the larger of the two values, $x or $y, will be returned.
As defined, the above function could be used in comparisons such as announcing sibling ages and determine the age gap. If this was done for two sets of siblings, the results can be used in another function:
$difference = comparison(13,5) - comparison(6,3);
echo "the difference is $difference"
As illustrated in this example, using a return instead of printing the result allows the result to be used in the $difference computation that follows.
Functions can return values without parameters.
A time-based greeting:
Write a simple function called 'greeting' with no parameters that prints out a salutation. Have the greeting include the day of the week, utilizing the function:
date('l');
Your output should be similar to this: "Hello, today is Wednesday"
Create a function that rolls a die via a random number generator. Return the value of each roll.
rand(1,6)
Roll the die three times and return the value of the roll to a running tally of all three roll outcomes. Print the sum of the three rolls once complete.
Expand on the last question. Remove the line where the sum of rolls is printed.
Call the function twice.
Create a comparison function that checks which of the two sums from part a is larger and returns the higher number as the winning value.
Below is an example of a declared function used to determine the area of a triangle:
function area($width, $height){
return $width * $height / 2;
}
In this example, two values are specified as parameters, and a resulting value is computed and return in a single statement.
Another example returns the sum of two numbers:
function add($number1, $number2){
$result = $number1 + $number2;
return $result;
}
In this example, two values are identified as parameters, and computation involves some processing before a final answer is returned.
Write a function that has year, month, and day (in that order) as parameters and returns a date string of the form MM-DD-YYYY. (Although this problem may seem conceptually straight forward, both the month and day require two digits, so the integer 1 should appear as 01.)
Consider sets of numbers, such as the following, as RGB color values.
In considering such triples, some checking and correction may be necessary to obtain color numbers between 0 and 255.
With this background:
Write a function with three integers as parameters that returns the three numbers for each set as strings in the format of an RGB specification: "rgb(--,--,--)"
Use this function to create some element of an HTML page that uses the color as a background for text of your choosing.
Subtraction:
Write a function subtract that accepts two values, subtracts one value from the other, and returns the result.
Use your function from part a with 50 and 30 as the parameters:
Write a new function that has four parameters: $a, $b, $c and $d and computes
(($a-$b)-$c)-$d
Compute this expression in just one line, by using calls to subtract as parameters to subtract itself.
The formulae for the surface areas of various objects utilize various measurements. Three examples follow:
| Object | Measurements | Area Formula |
|---|---|---|
| circle | radius (R) | π * R2 (where π is about 3.1416) |
| rectangle | height (H), width (W) | H * W |
| box | height (H), width (W), length (L) | 2*(H*W+H*L+W*L) |
Write a function that has an array of values as a parameter.
Hint: If $arr is an array variable, then either sizeof($arr) or count($arr) returns the number of elements in the array.
A car dealership has inventory on hand and a customer in the reception area. For example, a customer might have a budget of $15,000 and would like to buy a car. The current inventory spreadsheet for the dealership, based on car color, is shown below:
| Color | Quantity | MSRP |
|---|---|---|
| Black | 13 | $13,400 |
| White | 16 | $16,500 |
| Red | 20 | $17,500 |
| Blue | 8 | $17,200 |
| Silver | 9 | $18,900 |
Within this context, a customer has a budget of $15,000 and would like to buy a car.
Comparing budget and costs:
Put the data from the first and third columns of the table into an associative array with the color as the key and the MSRP as the array values.
Write a function that accepts the customer's budget and the array of inventory as parameters.
Search the array to determine which car color(s) are within the customer's budget.
Return the car colors that are within the customer's price range in a string.
Write a function that fulfills the same tasks as question 6, except with these changes:
The dealership is trying to turn over stock, and is offering 10% off the MSRP for colors with a quantity higher than 12 vehicles. Refine your function from Problem 10 to make cars that meet this stipulation 90% of their original price. Then return an array containing the car colors within the price range.
The Gregorian calendar has asymmetric rules for the number of days in a month:
With these rules, for most months, it may be helpful (but not required) to create an associative array with the month name as the keys and the number of days as the values. (Likely February will have to be handled as a special case, regardless of how processing of other months proceeds.
Write a function that accepts the string value $month and adds up the total number of days in the year that have already happened (call it $totalDays). The function should return a string of the form:
Return "we are" + $totalDays + "into the year"
(Challenge Problem)
Write a function that has year, month, and day (in that order) as parameters and returns the next date in MM-DD-YYYY format.
Notes:
|
created 5 March 2019 updated and expanded 9-15 March 2019 |
|