R. Chris Fraley Department of Psychology 603 East Daniel Street Champaign, IL 61820


"; } print "Here is some text. We call the function footer simply by using footer()
"; footer(); //--------------------------------------------------------------------------- /* We can also use arguments/parameters in the function call. Moreover, we can have the function RETURN some output that can be assigned to a variable. Here is an example. */ function add_this($n1, $n2){ $sum = $n1 + $n2; return $sum; } $result = add_this(3,10); print "

The returned value from the call \$result = add_this(3,10) is $result

"; print "

Note: the values \$n1 and \$n2 do not exist OUTSIDE the function if they are not explictly returned: \$n1 = $n1 and \$n = $n2.

"; //--------------------------------------------------------------------------- /* Let's make this more general. This function will average a set of scores. It doesn't matter how many scores there are (this is dynamic). The array containing the scores can be taken as an input parameter. */ function average_scores($score_array){ $sum = 0; $n = 0; foreach($score_array as $key => $value){ $sum = $sum + $value; $n = $n + 1; } $average = $sum/$n; $average = round($average,2); return $average; } $scores = array(1,1,1,2,3,4,3,4); $results = average_scores($scores); print "

Here is the result from averaging 1 1 1 2 3 4 3 4: $results

"; ?>