Practice Exercise: Programmer-defined Functions
Objective: Be able to create and invoke a PHP function.
The general form for a function is:
function functionName ( [formal parameter list] ) {
statements
[return expression;]
}
The general form for a function call is:
[variable = ] functionName( [arguments or actual parameter list] );
0. Define a void* function named printTitle that receives no parameters
but prints a report title line containing the string "Motocloz List".
*does not return a value
1. Prepare a function named printDetail with a value parameter for item[] fields
(id, name, cost), and a reference parameter(&arg) for $costs[]. Assign the
$item[] elements to the fields, insert $cost into $costs[], and print the
item fields.
2. Define a value-returning function named sumCosts. This function should have
a formal parameter for an array named $costs. The function body will need a
statement to initialize the floating point variable $sum to zero and a
foreach loop to iterate through the array summing $costs[] (as $c). The
function should return the value of $sum.
3. Define a void function named printFooting that receives the parameter
$totalCost. It should print a line with the side label "total:" and
the value referenced by $totalCost decimal-aligned with the cost column
in the body of the report.
4. Invoke the function named printTitle.
5. Within the "main" block of code, prepare a loop to control access to
"item.csv" file records and call printDetail().
6. Invoke sumCosts, pass it the array named $costs, then assign the returned
value to $totalCost.
7. Invoke printFooting and pass it the variable $totalCost.