PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Function Call Operations

This section describes the function call operation, which triggers the execution of the called function with arguments provided on the call.

Calling a function in PHP is simple. All you have to do is to just use the function name with an argument list in any expression:

   ... function_name(argument_1, argument_2, ...) ...

When the execution reaches this function call operation, it will:

  • Evaluate all calling arguments to values or variable references.
  • Assign argument values and references to argument variables defined in the "function" statement.
  • Start to execute the first statement of the function body.
  • Continue to execute the function body until the end of the function body or a "return" statement.
  • If a "return" statement is reached with a value specified, that value is returned as this function call operation.
  • If a "return" statement is reached with a no value specified, NULL is returned as this function call operation.
  • If no "return" statement is reached, NULL is returned as this function call operation.

Here is a simple example script showing us how to use function call operations:

<?php # FunctionCallOperations.php
# Copyright (c) 2003 by Dr. Herong Yang. http://www.herongyang.com/
#

#  Defining a function that returns a value
   function f2c($fahrenheit) {
      $celsius = ($fahrenheit - 32.0 ) / 1.8;
      return $celsius;
   }

#  Defining a function that returns NULL
   function showAnyFooter($author) {
      print("    Copyright: $author\n");
      print("    Last Modified: 2003\n");
   }

   print("\n Function call as a standalone expression:\n");
   showAnyFooter("Herong Yang");

   print("\n Function call as an operand of another operation:\n");
   if (f2c(20.0)<0.0) print("    It's cold here!\n");

   print("\n Function call as an argument of another call:\n");
   print("    How is the temperature? ");
   print(f2c(110.0));
?>

If you run this sample script, you should get:

 Function call as a standalone expression:
    Copyright: Herong Yang
    Last Modified: 2003

 Function call as an operand of another operation:
    It's cold here!

 Function call as an argument of another call:
    How is the temperature? 43.3333333333

Last update: 2005.

Sections in This Chapter

What Is a Function?

"function" Statements - Defining Functions

Function Call Operations

Passing Arguments to Functions

Example of Passing Arguments by Values

Using Pass-by-Value Arguments for References

Example of Passing Arguments by References

Variable-Length Argument Lists

Providing Default Values to Argument Variables

Returning Values from Functions

Returning References from Functions

Dr. Herong Yang, updated in 2009
Function Call Operations