Return to this week's Bulletin

 

 



28 December 2001

The Clink Function

by Joseph DiVerdi

It was my first Web Programming class after Thanksgiving and the subject was "Perl Subroutines & PHP Functions". As is popular in programming courses, I thought the factorial function would serve as an suitable example for the class. Once I explained that the factorial of any non-negative integer is defined as the product of all integers from one to the chosen integer, inclusive, I added in passing that I had thought about the factorial function over Thanksgiving dinner. Some students looked up at me. I continued that I always think about the factorial function at dinners and many other social occasions. They now looked at me as if I had just grown another head. The explanation for my obscure thoughts is really quite simple: I come from a family where everyone at the table is obliged to "clink" glasses after a toast with everyone else. The family is also rather large and when a toast is being made I often ponder exactly how many clinks I can expect to hear in the next moments. I refer to that calculation as the "clink function" and the factorial function is the foundation of it.

It's easy to compute the factorial of small integers. For example, 3 factorial, written 3!, is 3 * 2 * 1 = 6, while 5! = 5 * 4 * 3 * 2 * 1 = 120, and so on. Now the number of clinks is NOT the factorial of the number of people at the table, we'll come back to that later. By the way, 0! is a special case and equals one by definition. The factorial function is undefined for non-integers and for negative integers.

While it's fairly easy to compute the factorial of a small integer it is also easy to see how manually computing the factorial of larger numbers can be very tedious. Consider computing 100!, the exclamation point takes on more significance here. However, this computation is very well suited to a computer and can be programmed into a function or subroutine in two very different ways: direct computation and recursive computation. For now, let's consider the direct method and leave recursion for some other time.

One possible Perl function which directly computes the factorial is given by the following code snippet and described in the following paragraph:

subroutine factorial {

$arg = shift;
$temp = 1;

foreach $i (1..$arg) {

$temp = $temp * $i;
}

return $temp;

}

Perl variables always begin with a dollar sign to distinguish them from simple strings. "shift" means to grab the argument submitted to the function. The "foreach" construct creates a loop which repeatedly executes the contents of the curly braces while assigning $i with each integer from one to the value of $argument. The return statement returns the computed product to the calling program. There are many ways to program this function and many languages in which to write it.

While the factorial function is not our ultimate quest it is very useful. If we decide to photograph the family members all in one line, the factorial of the number of photographees will give us the total number of possible arrangements we can have in the photograph, like Grandma on the left, Uncle Vito next, Cousin Donnetalla, next... or Cousin Donnetalla on the left, Grandma next, Uncle Vito next... With just five of us there are 5! or 120 possible arrangements - with ten there are 10! or 3,628,800. Now you know why it is so difficult to arrange family photographs.

More in a future installment...