CS 199 Willamette University Spring, 2019
 
Programming in PHP, Databases with MySQL,
and Web Applications
 

Lab: Functions - Return Statements

The return statement is used for several purposes, including

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:

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.

Return Statements without Parameters:

Functions can return values without parameters.

  1. A time-based greeting:

    1. 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"

    2. PHP's date function, given a parameter "G", also can return the current hour using a 24-hour clock. That is, date("G") returns a number between 0 and 24. Expand your function for part a by replacing "Hello" with a time of day:
      • if the hour is between 5 and 12, use "Good morning"
      • if the hour is between 12 and 18, use "Good afternoon"
      • if the hour is between 18 and 21, use "Good evening"
      • if the hour is after 21 or before 5, use "Good night"
  2. Create a function that rolls a die via a random number generator. Return the value of each roll.

          rand(1,6)
        
  3. 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.

  4. Expand on the last question. Remove the line where the sum of rolls is printed.

    1. Call the function twice.

    2. 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.

Return Statements with Parameters:

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.

  1. 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.)

  2. 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:

    1. 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(--,--,--)"

    2. Use this function to create some element of an HTML page that uses the color as a background for text of your choosing.

  3. Subtraction:

    1. Write a function subtract that accepts two values, subtracts one value from the other, and returns the result.

    2. Use your function from part a with 50 and 30 as the parameters:

    3. 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.

Using Functions with Arrays

  1. 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.

Dealership Inventory

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.

  1. Comparing budget and costs:

    1. 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.

    2. Write a function that accepts the customer's budget and the array of inventory as parameters.

    3. Search the array to determine which car color(s) are within the customer's budget.

    4. Return the car colors that are within the customer's price range in a string.

  2. Write a function that fulfills the same tasks as question 6, except with these changes:

    1. Declare the array within the function rather than passing it as a parameter.
    2. Return the car colors in an array, rather than a string.
  3. 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

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.

  1. 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"
        
  2. (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
Valid HTML 4.01! Valid CSS!