January 20, 2010
Conjunction Junction What’s your Function
Today I’ve decided to go over functions, (also known as “methods” or “subroutines”) the building blocks of modular code; and show why it’s a good idea to use them in your programming.
When I first started writing web applications I didn’t really understand what a function was and how it worked, so I just avoided them. I would instead write something procedurally and if I needed to do the same thing over again I would repeat that line (or lines) of code again.
Doing this breaks one of the important ethos of programming called DRY (“Don’t Repeat Yourself”). In a program of any sufficient complexity, repetition of code is NOT your friend.
Let me demonstrate how using a function can save you from repeating lines of code over and over.
Here is a section of PHP code that calculates a human readable string of days, hours, minutes, and seconds from two times to give a files age. I want to get the plurals of day(s), hour(s), etc., correct. So I am testing the number to see if there is more than one and adding an “s” to the end if there is.
<?php
//...
$seconds = strtotime($curdate) - strtotime($olddate);
/*** number of days ***/
$days=(int)($seconds/86400);
/*** number of hours ***/
$hours = (int)(($seconds-($days*86400))/3600);
/*** number of mins ***/
$mins = (int)(($seconds-$days*86400-$hours*3600)/60);
/*** number of seconds ***/
$secs = (int)($seconds - ($days*86400)-($hours*3600)-($mins*60));
/*** return the string ***/
if($days != 0) {
if($days > 1) {
$plural = "s ";
} else {
$plural = " ";
}
$file_age = $days . " day" . $plural;
}
if($hours != 0) {
if($hours > 1) {
$plural = "s ";
} else {
$plural = " ";
}
$file_age .= $hours . " hr" . $plural;
}
if($mins != 0) {
if($mins > 1) {
$plural = "s ";
} else {
$plural = " ";
}
$file_age .= $mins . " min" . $plural;
}
if($secs != 0) {
if($secs > 1) {
$plural = "s ";
} else {
$plural = " ";
}
$file_age .= $secs . " sec" . $plural;
}
echo $file_age;
//...
?>
Using a function to simplify the getting of the plural distills the 20+ lines of code into one function making it in essence 5 lines of code.
<?php
//...
function getPlural($num) {
/*** if more than one ***/
if($num > 1) {
$plural = "s ";
} else {
$plural = " ";
}
return $plural;
}
function calculateFileAge($curdate, $olddate) {
/*** get difference in times in seconds ***/
$seconds = strtotime($curdate) - strtotime($olddate);
/*** number of days ***/
$days=(int)($seconds/86400);
/*** number of hours ***/
$hours = (int)(($seconds-($days*86400))/3600);
/*** number of mins ***/
$mins = (int)(($seconds-$days*86400-$hours*3600)/60);
/*** number of seconds ***/
$secs = (int)($seconds - ($days*86400)-($hours*3600)-($mins*60));
/*** return the string ***/
if($days != 0) {
$file_age = $days . " day" . getPlural($days);
}
if($hours != 0) {
$file_age .= $hours . " hr" . getPlural($hours);
}
if($mins != 0) {
$file_age .= $mins . " min" . getPlural($mins);
}
if($secs != 0) {
$file_age .= $secs . " sec" . getPlural($secs);
}
return $file_age;
}
//...
?>
This function called getPlural() is a very simple function we pass the function one “argument” (one independent variable that is the input to a function). In our case that argument is the $num variable. In the function definition (where we define function getPlural()), $num is just a placeholder for whatever variable we input. In the example we input to the function a number of days, hours, minutes, and seconds.
The function does the work of examining the number and then returning a single variable that either is “s” or an empty string.
The nice thing about designing a function is that once your function does it’s job; namely taking arguments and returning a result. It is a nice black box which you don’t have to think about how it operates any more and you can use it over and over again without repeating the code that is contained therein.
Functions are a great tool for helping you to stay original and stop repeating yourself.

Very interesting Josh…. I can use some of this in my Linux/Unix scripting class coming up.
Comment by Aaron — March 9, 2010 @ 11:17 pm