Reasons Why You Should Use a Function
When is a good time to use a function or a method? Here is a list of reasons when you should use them.
Reduce Complexity
This is probably the most important of all reasons to use a function. You can create a function and forget about its inner workings. This creates an abstraction of all the code within it by hiding the information every time you use the function. Not to mention it helps with code size, maintainability, and correctness.
Example:
1 |
//Complex ———————–
if ($val >= 32 || $val < 10){
for($i =0; $i < 10; $i++){
echo “name”. $i
}
}else{
echo “no name”;
}
//Simple ———————–
echo getName($val);
?>
Make Code Easier To Read
Putting a piece of code within a well named function is one of the best ways to document your code. Instead of reading multiple lines of code you can simply read a single descriptive line of code.
Example:
1 |
// Difficult to read…it takes time to
//figure out what is going on.
$names = array(‘Bob’,'Ted’,'Bill’);
for ($i = 0; $i < count($names); $i++){
echo $names[$i];
}
//Self explanitory code.
DisplayNames();
?>
Avoid Duplicate Code
If you find that you have several instances of the same code spread through out, it is probably a good indication you should use a function in its place.
Example:
1 |
function getUserName(){
$firstname = getFirstName();
$lastname = getLastName();
$name = $firstname . ” ” . $lastname;
return $name;
}
// Functions prevents having to use duplicate code.
echo “Welcome “. getUserName() . “!”;
echo “What kind of name is “. getUserName() . “?”;
?>
Hide Sequences
It is usually a good idea to hide the order in which things happen. If a program has a set of processes it has to go through, it’s best that each process doesn’t have to rely on any others to work. Placing each process in it’s own function will help keep things organized.
Example:
1 2 3 4 5 6 | /* * Splitting up the process into functions, * each process doesn't know what the other * one is doing. This allows you to make changes * to the process with little to no trouble. */ |
$userName = getUsername();
$validUsername = checkUsername($userName);
updateDatabase($validUsername);
?>
Improve Portability
Use functions to help portability by isolating code into nice little packages.
Example:
1 2 3 | function add ($a, $b){ return $a + $b; } |
// First lame program
echo add(1,7);
//Function works just as well in any program…as lame as it might be. ![]()
for ($i = 0; $i
echo $;
}
?>
Simplify complicated Boolean Tests
If you have an overly complicated Boolean test, placing the test within a function can help make your condition statements a lot easier to read, and use.
Example:
1 |
//Complicated
if ((2*5+3/7)/2 > 30){
echo “wtf?”;
}
//Simplified
if (isValidNumber()){
echo “Ahh much better.”;
}
?>
Improve Performance
Instead of running around trying to optimize your code in several places, functions allow you to place your code into one place. This way you can optimize your code in one place and it will take effect in several areas, in turn making your life easier.

