1: |
True or False: If a function doesn't require an argument, you can omit the parentheses in the function call.
|
2: |
How do you return a value from a function?
|
3: |
What would the following code fragment print to the browser?
$number = 50;
function tenTimes() {
$number = $number * 10;
}
tenTimes();
print $number;
|
4: |
What would the following code fragment print to the browser?
$number = 50;
function tenTimes() {
global $number;
$number = $number * 10;
}
tenTimes();
print $number;
|
5: |
What would the following code fragment print to the browser?
$number = 50;
function tenTimes( $n ) {
$n = $n * 10;
}
tenTimes( $number );
print $number;
|
6: |
What would the following code fragment print to the browser?
$number = 50;
function tenTimes( &$n ) {
$n = $n * 10;
}
tenTimes( $number );
print $number;
|
| |
A1:
| The statement is false. You must always include the parentheses in your function calls, whether or not you are passing arguments to the function. |
| |
A2:
| You must use the return keyword. |
| |
A3:
| It would print 50. The tenTimes() function has no access to the global $number variable. When it is called, it manipulates its own local $number variable. |
| |
A4:
| It would print 500. We have used the global statement, which gives the tenTimes() function access to the $number variable. |
| |
A5:
| It would print 50. When we pass an argument to the tenTimes() function, it is passed by value. In other words, a copy is placed in the parameter variable $n. Any changes we make to $n have no effect on the $number variable. |
| |
A6:
| It would print 500. By adding the ampersand to the parameter variable $n, we ensure that this argument is passed by reference. $n and $number point to the same value, so any changes to $n are reflected when you access $number. |