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

"function" Statements - Defining Functions

This section describes the 'function' statement, which can be used to define your own function with a function name, argument list, function body.

A "function" statement defines a function with the following syntax:

   function function_name($argument_1, $argument_2, ...) {
      statement_1;
      statement_2;
      ...
   }

where "function_name" is the name of the function, and "$argument_1, $argument_2, ..." is a list of variables acting as arguments of the function. "statement_1; statement_2; ..." is a block of statements that becomes the body of the function.

The "function_name" is the identifier of the function, it must follow PHP identifier naming conventions. The general rule is that don't use any special characters in function names.

The argument list is optional. If not needed, define your function with no arguments but keep the argument parentheses like this:

   function function_name() {
      statement_1;
      statement_2;
      ...
   }

When a function is called in an expression, it will always return a value for the expression. The returning value can be controlled in 3 ways:

1. If the function body is executed to the last statement without hitting any "return" statement, the default value, NULL, will be returned to the calling expression.

2. If an empty "return" statement is reached during the function body execution, the remaining part of the function body will not be executed and the default value, NULL, will be returned to the calling expression.

3. If a "return" statement with an expression, like "return expression", is reached during the function body execution, the remaining part of the function body will not be executed and the resulting value of the specified expression will be returned to the calling expression.

To help us understand how to use "function" statements, I wrote the following the tutorial example PHP script:

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

#  Defining a real dummy function
   function dummy() {}
   
#  Defining a function with no arguments
   function showMyFooter() {
      print("Copyright: Dr. Herong Yang\n");
      print("Last Modified: 2003\n");
   }

#  Defining a function with arguments, but doing nothing
   function doNothing($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k) {}

#  Defining a function with arguments and doing something
   function showAnyFooter($author) {
      print("Copyright: $author\n");
      print("Last Modified: 2003\n");
   }

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

#  Defining a function that returns a random value
   function getValue() {
      $i = rand(1, 6);
      if ($i==1) {
         return TRUE;
      } elseif ($i==2) {
         return 9;
      } elseif ($i==3) {
         return 9.99;
      } elseif ($i==4) {
         return "Hello";
      } elseif ($i==5) {
         return;
      }
   }
?>

If you run this sample script, you will get no output, because this script only defines those functions. There is no expression that calls any of those functions.

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" Statements - Defining Functions