Skip navigation.
Allow sufficient time for real learning

Chapter 13 Hints and Help

QUESTION: Can I include more than one return statement in a function?

ANSWER: You can include more than one return statement but only if the function includes control structures to ensure that only a single return statement will execute each time the function is called. For example, if a function named passFail() is intended to return either "pass" or "fail" depending on a score that is received, you could write this as follows:

function passFail ($score)
{
   if ($score >= 60)
      return "pass";
   else
      return "fail";
}

In this case, there are two return statements but only one will execute each time the function is called, depending on the score. You could also write this function with just one return statement:

function passFail ($score)
{
   if ($score >= 60)
      $result = "pass";
   else
      $result = "fail";
   return $result;
}

Another approach would be to rewrite the function with a name like passingScore and design it to simply test if the score is a passing score and return true or false:

function passFail ($score)
{
   return $score >= 60;
}

Now the code that calls the function can decide how to handle the result, for example:

if (passingScore($score))
   print("You have passed the test.");
else
   print("You did not pass the test, take some time to review the material and let me know if you need some help.");

QUESTION: Do I have to call a function using the same variable names as those listed in the function heading?

ANSWER: No, this is a common misunderstanding. The variables listed in the function heading are known as the function's parameters. These variables are used to receive whatever values are sent to the function (these values are known as arguments), in the same order that the values are sent. That means you must send the correct number of values to the function, one value for each parameter, and these must be sent in the same orer to match the parameters. But these arguments do not need to be variables with the same names as the function's parameters.

For example, here is a simple function named getPay() that receives a number of hours worked and an hourly wage. The function calculates and returns the pay:

function getPay ($hours, $hourlyWage)
{
   return $hours * $hourlyWage;
}

The getPay() parameters are named $hours and $hourlyWage. Now here are five examples where we call this function using diferent values (arguments) for the hours worked and hourly wage:

$pay1 = getPay(30, 10.75)
$pay2 = getPay($hoursWorked, 15.50)
$pay3 = getPay($hoursWorked, $payRate)
$pay4 = getPay(30, $payRate)
$pay5 = getPay($hours, $hourlyWage)

(In this example, we are assuming that the calling program has already assigned values to the variables $hoursWorked, $payRate, $hours, and $hourlyWage.)

In the first statement, getPay() is called with 30 and 10.75 as arguments. So 30 is received by the function's $hours parameter and 10.75 is received by the $hourlyWage parameter. The function then uses these two parameters to calculate and return the pay.

In the second statement, getPay() is called with the variable $hoursWorked and 15.50 as arguments. So the value stored in $hoursWorked is received by the function's $hours parameter and 15.50 is received by the $hourlyWage parameter. The function then uses these two parameters to calculate and return the pay.

In the third statement, getPay() is called with the variable $hoursWorked and the variable $payRate as arguments. So the value stored in $hoursWorked is received by the function's $hoursparameter and the value stored in $payRate is received by the $hourlyWage parameter. The function then uses these two parameters to calculate and return the pay.

In the fourth statement, getPay() is called with the value 30 and the variable $payRate as arguments. So 30 is received by the function's $hours parameter and the value stored in $payRate is received by the $hourlyWage parameter. The function then uses these two parameters to calculate and return the pay.

In the fifth statement, getPay() is called with the variable $hours and the variable $hourlyWage as arguments. So the value stored in $hours is received by the function's $hours parameter and the value stored in $hourlyWage is received by the $hourlyWage parameter. The function then uses these two parameters to calculate and return the pay.

Note that, in the last example, the program that calls the function uses the same two variable names that are used for the function parameters. This makes no difference, they are in fact different variables. In every case the values (not the variables) are passed as arguments to the function parameters and the values are processed inside the function by referring to these parameters.