A function is a part of independent code that can be use to perform any specific task. Function are use to get the required output. The output entirely depends on the code you specified in it. You can call the same function as many time as you want in the same page. A function does the fallowing things:

Accept value

A function accept values from the argument passed to it. But this is an optional part. You may not assign any value to a function.

Process value

A function executes the code specified within it.

Perform an action

Lastly this is the output that returns after the function is executed.

The advantage of using function in code are that, a function makes a code simple to read and understand and also ensure reusability of codes. This is because the same function can be called several times. To use a particular piece of code specified within a function, the developers just need to call the specific function.

Features of PHP functions

- Function are self contained .That is they are self sufficient blocks of code that can operate independently.
- A function needs to called in a spirit to be executed.
- Values can be passed with functions.
- A function can be declared and hen executed whenever required.
- A particular function can be called several times within the same page.
- A function can consist of PHP code, other functions, or a class definition.
- A function cannot be redefined after it is declared.

User defined versus built in function

In PHP ,function can be broadly categorized as user defined or built in functions. User define functions as the name suggest, are function created by a user as per requirement so the programmer can completely customize.

Unlike user defined functions, built in function are prewritten functions that are part of PHP. These functions have a predetermined functionality that doesn't change. There are hundreds of built in functions available with PHP.

How to Define a Function

<?php
function MyFunction($value1, $value2){
 /* write your code here */
}
?>

Create a Simple Addition Function


<?php
function addMe($value1, $value2){
 $result = $value1 + $value2;
 return $result;
}
/* Call in action */
print addMe('6','3');

?>

The Output will be : 9.
Top