Lecture 31: Simple Arguments
Functions can be incredibly powerful, because you can program them to do almost anything you want them to. One important step in making your functions even smarter is to use "arguments" within your function. Not the type of argument where you and a friend are bickering about a disagreement, I'm talking about arguments within functions — two very different things.
Think of an argument like a variable. Your program can pass extra information to your functions using arguments. You specify your arguments within the parenthesis after your function name, and you can have as many as you want, as long as they're comma separated.
Let's look at an example of a function with a single argument:
function hangTen($location) {
echo "We're surfing in $location!";
}
hangTen("Hawaii");
hangTen("California");
hangTen("Newfoundland");
So, in the above example, we're passing an argument to the hangTen() function. Later in our script, we call our function several times with a string of text within each parenthesis, and each time we call our function with a new argument value, that value will display on the screen along with the text we provided in our function.
The output will look like this:
We're surfing in Hawaii!
We're surfing in California!
We're surfing in Newfoundland!
Let's look at an example with two arguments:
function multiplyTogether($val1, $val2) {
$product = $val1 * $val2;
echo "The product of the two numbers is: $product";
}
multiplyTogether(14, 27);
All we did here was add another argument, separated by comma, then when we called our multiplyTogether() function later in the script, we provided two values to take the place of our $val1 and $val2 arguments.
The result?
The product of the two numbers is: 378
DOWNLOAD COURSE FILES HERE
[ Ссылка ]
Ещё видео!