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

Function Declaration, Arguments, and Return Values

This chapter provides tutorial examples and notes about user-defined functions. Topics include defining function with arguments, calling functions in expressions, passing arguments by values or by references, variable-length argument list, providing default values for arguments, returning data by value or by reference.

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

Conclusion:

  • A "function" statement defines a function with or without argument variables.
  • A "return" statement in a function stops the execution of the function and returns a value to the calling code.
  • Function call option can provide more arguments than what are defined in the function.
  • "func_get_arg()", "func_get_args()", and "func_num_args()" allows us to access arguments without using argument variables.
  • An argument can be passed by references if you add "&" to the argument variable.
  • A function can return the reference of a variable, if you add "&" to the function name.

Dr. Herong Yang, updated in 2009
Function Declaration, Arguments, and Return Values