| CS 199 | Willamette University | Spring, 2019 |
|
Programming in PHP, Databases with MySQL, and Web Applications |
||
Your textbook, pp. 216-246, discusses how a segment of code can be separated from the rest of a program into a logical, operational unit called a function. Typically, a function is defined for any of several reasons, including the following:
The same section of code is needed in several places, just as the chorus of a song may be repeated after each song verse. Functions allow one to write the code once and then re-use it many times.
Once section of code may seem relatively long and complicated, and looking at all of the details may obscure the overall logic of the main program. As an analogy, an outline for a paper might take the form
Although such organization may be clear within a Table of Contents, details of section II.A.iii. may distract the reader from the overall development of the narrative. In such cases, a separate section of a paper (or a PHP function) may encapsulate the details, so the reader can focus on the main steps.
Sometimes the same formulae and logical steps must be followed, but based on different starting values. Once the values are given, processing proceeds in exactly the same way.
As discussed in the textbook, functions can help with each of these circumstances.
In the last case, where the same formulae and logical steps are followed with different values, the start of a code segment gives a name to the function, and also gives labels to the starting values. In the following example, a function is given the name printProduct, and work depends upon two starting values, called $first and $second:
function printProduct ($first, $second) {
$product = $first * $second;
echo "the product of $first and $second is $product\n";
}
Within a main program, this same sequence of steps can be used several times, as in the following code:
<?php
printProduct (3, 7);
printProduct (5, 12);
$a = 4;
$b = 8;
printProduct ($a, $b);
?>
When this code segment is run, the three uses of printProduct yield the following output:
the product of 3 and 7 is 21 the product of 5 and 12 is 60 the product of 4 and 8 is 32
In each case, the values specified (e.g., 3 and 7, or 5 and 12, or 4 and 8) are used for the function values $first and $second, and the function's code is then followed. Note that in plugging these values into functions, the first value specified in the main program (e.g., 3 or 5 or 4) is used as the first function value (e.g., $first, and the second value specified in the main program (e.g., 7 or 12 or 8) is used for the second function value (e.g., $second).
When defining a function, the values in parentheses are identified by several names. For example, one common term, used by the textbook, is arguments, and another common term in widespread use, is parameters. Thus, in the above function definition:
function printProduct ($first, $second)
$first and $second are called either arguments or parameters.
In some contexts, it may be useful to distinguish between the names used in the function and the values being passed in. In making such distinctions, the function names (e.g., $first and $second) are called formal parameters, and the values being passed in from the main program (e.g., (3, 7) or (5, 12) or (4, 8)) are called actual parameters.
For consistency throughout this and future labs, we use parameters or actual parameters when referring to these values identified when a function is defined.
Consider the following function:
function add($num1, $num2){
$num3 = $num1 + $num2;
echo "$num1 + $num2 = $num3";
}
Next, review function myName,
function myName($firstName, $lastName){
echo "Hello, my name is $firstName $lastName";
}
Create a function that calculates the circumference of a circle. Let the parameter be a radius, and let pi = 3.14. (Hint: circumference = 2*pi*radius)
Create a function that calculates distance using the equation: distance = rate * time.
Create a function that inputs someones favorite animal and prints out "My favorite animal is a $animal".
Create a function that inputs a number. Within the function, it should check if the number is odd or even. Depending on the number it should print "$num is even" or "$num is odd".
Create a function that has an array of numbers as a parameter and then calculates the average of all the numbers in the inputted array. Print out the average.
Typically, clothing sizes will run from 00 through 20. Some stores have an alternate labeling of sizes. Create a function based off the following table of women pant sizes that allows a user to enter in their numeric pant size and print out if they are a XXS, XS, S, M, ... etc.
Numeric Pant Size Size
----------------- ------
00 XXS
0-2 XS
4-6 S
8-10 M
12-14 L
16-18 XL
19-22 XXL
Create a function has a color as parameter and then prints some text in a paragraph that utilizes that color for the background.
Now create a function that has color, text, font style, and font size as parameters and prints out your text in this style on the background function you previously created.
Why is it helpful to create function as opposed to just writing lines of code? What are advantages?
|
created 5 March 2019 updated and expanded 9-12 March 2019 |
|