The concept of globalization, functions and arguments can be confusing to those who are new to programming. It’s not too complicated if you look at a very basic example. The following function isn’t very practical, but it works.
function addThis($a,$b) {
return $a + $b;
}
$sum_of_these = addThis(8,3); // assigns the value of 11 to the variable $sum_of_these
?>
In the above example we are passing by argument the numbers 8 and 3 to the function called addThis and that function is returning the value of 11 (which is 8 + 3) and then assigns the value of 11 to $sum_of_these variable. This would be the same as writing the following without using any function:
Now, let’s say we want to add in a third variable, called $c to the equation. How can we do this in the function addThis without passing it as an argument? We do this by making the $c variable globally available inside the function addThis like this:
function addThis($a,$b) {
global $c; // separate multiple globals by a comma like this: global $c, $d, $e;
return $a + $b + $c;
}
$c = 4; // first must assign some value to $c
$sum_of_these = addThis(8,3); // assigns the value of 15 to the variable $sum_of_these
?>
Now let’s say we don’t want to use any arguments at all, let’s globalize $a, $b and $c. This code would look as follows:
function addThis() {
global $a,$b,$c; // separate multiple globals by a comma and end with a semi-colon
return $a + $b + $c;
}
$a = 8;
$b = 3;
$c = 4;
$sum_of_these = addThis(); // assigns the value of 15 to the variable $sum_of_these
?>
Still the use of a function above doesn’t make practical sense, but it shows a very rudimentary example of how to use functions, arguments and globals. Now let’s make a better example of a simple calculator:
function simpleCalc($operator) {
global $a,$b,$c; // separate multiple globals by a comma like this: global $c, $d, $e;
switch($operator) {
case ‘-’:
return $a - $b - $c;
break;
case ‘*’:
return $a * $b * c;
break;
case ‘/’:
return $a / $b / $c;
break;
default:
return $a + $b + $c;
}
}
$a = 8;
$b = 3;
$c = 4;
$result = simpleCalc(‘+’); // assigns the value of 15 to the variable $sum_of_these
echo($result);
?>
The above example allows us the flexibility of sending along any three numbers and either add, subtract, multiply or divide. I used a switch statement to illustrate how to check for addition (+), subtraction (-), multiplication (*) or division (/). Readers will note that the default switch is addition, so there is no need for a case block for that.
So if we wanted to multiply the three variables we’d use the following code:
Subtraction, just pass along the ‘-’ like this:
This is a real crude and impractical calculator because a user could enter in a combination of more than three numbers and even multiple operators, but it clearly demonstrates how to pass and return arguments in functions, how to globalize variables and how to perform different operations based on conditions. At the most basic level that is all a calculator does: it takes input and processes.
I’ll leave this type of calculator for a more advanced tutorial (a Part 2, perhaps) or maybe readers would like to build their own? Basically to accomplish a full calculator, the programmer would need to dump all numbers and operators into an array and work through that in the function to return the value. This might be kind of fun for a quick and basic JavaScript app. Don’t even need server side PHP code for this, it could be done entirely in JavaScript in a step-by-step process because a calculator is somethng most everybody understands.

Thanks fo rthe great example. This really helped me out creating some of my own user defined functions!
on February 13th, 2006 at 11:12 am | #Link Comment