A function is a unit of a program or a block of code consisting of a set of statements. A function generally performs a specific task and it has a specific purpose in a program. Functions play an important role in programming because instead of writing repetitive and redundant code for frequently performed tasks we can just define a function and then we can call it whenever required.
A function in a PHP page will never get executed when the PHP page loads. It will get executed only when the function is called.
PHP offers more than a 1000 built-in functions. It also provides users with the facility to define their own functions because often users need to define their own custom functions.
There are 2 essential aspects of working with functions. They are
- Creating a PHP function
- Calling a PHP function
Creating a PHP function
A PHP function should be created or defined before we attempt calling it. Some of the rules to be observed while defining a PHP function are as follows
- The keyword function is used along with the name of the function while defining the name of the function.
- The name of the function should be an intuitive name because it should indicate the purpose of the function. We can then ensure that the function is easily reusable.
- All the statements of the function are enclosed within curly braces {}.
The basic syntax of creating a PHP function is shown below.
Code:
Example:
The example code shown below has a function printMessage defined in it. The printMessage function prints a simple message when it is called.
Code:
Functions with Parameters
Often we need to pass data to a function because the function may need to perform some operations on the data which is external to the function. Hence arguments are used. Arguments are the values that are passed to a function when we call it.
Arguments can only be passed to a parameterized function i.e. a function which has been defined to accept arguments. The rules to define parameters for a function are as follows.
- Arguments are defined after the function name within parenthesis.
- More than one argument can be defined. All the arguments are separated using commas.
- Arguments behave like normal variables inside a function and can be used as such.
Example:
The following function takes two parameters , adds them and prints the sum
Code:
Default Values for Function Parameters
Sometimes default values need to be set for the parameters of a function because the user may not pass any arguments while calling the function. Hence default values are assumed for the parameter values.
The following function prints NULL if the function does not pass any value.
Code:
Returning values from a PHP function
Functions can return a value using the return statement. A function can execute some statements and then it can return a simple value or an object. When the return statement executes it stops the execution of the function and sends the value specified in the return statement back to the calling script. A return statement consists of the return keyword along with the value to be returned.
Example:
The following example shows a function that accepts two parameters. It adds them and then returns the sum to the calling program. Here the return keyword is used to return a value from the function.
Code:
Output:
Value Returned from the function: 50
Passing Arguments by Reference
Passing an argument by reference is so called because we pass a reference to the variable as an argument. Hence the address of the variable is passed as a parameter while calling the function. Normally we pass arguments using the pass by value method where the copy of the variable value is passed to the function.
Passing arguments by reference is sometimes necessary because we need to manipulate and change the actual variables rather than copies of variables.
Hence any changes made to an argument which is passed by reference will change the value of the original variable.
We can pass an argument by reference by adding a '&' to the variable name in either the function definition or the function call. The function will then receive the address of the variable rather than a copy of the variable
Example:
The following example shows both pass by reference and pass by value
Code: