What is a Function?
In the last part of this tutorial series we looked at controlling logic flow
in PHP. This included logic flowing in a linear fashion, but with loops of
logic at various points and with decision making along the way. The
overall flow was a straight line - the loops were processed where they occurred,
and the logic continued along its line from the beginning of the program routine
to its end. There are times, however, when a series of instructions has to
be repeated from a variety of different points within the main routine, or where
a series of instructions needs to be followed that doesn't really fit within the
main routine. This all sounds horribly complicated -- let's see if a real
world example will clarify the picture!
Suppose, for the moment, you have a bunch of freshly washed laundry, some had
to be hung on a clothes line, some was dried in the drier. Now it all has
to be put away. Some of the items have to be folded, some ironed and some
hung on hangers. First we need a set of instructions for removing the
items from the clothes line, next a set for the drier. As each item is
removed from either source, we have to decide ("if" instructions, possibly?)
whether the item needs to be folded, ironed or hung. Then we need a set of
instructions for each of these operations -- folding, ironing and hanging.
The processes (folding, ironing and hanging) are the same whether the item was
removed from the clothes line or the drier. Consequently, we can
use the same folding, ironing and hanging routines whether we are calling them
from the "remove from the clothes line" process or the "remove from the drier"
process.
OK! Let's break it down! (And you thought you knew how to do the
laundry!) First, our main routine is going to have two primary sections --
one for the clothes line and one for the drier. As an item is removed, we have
to decide whether it should be folded, ironed or hanged, and process it
accordingly. Let's say that we know what "remove" means. This
allows us to say something like this (by the way, this kind of representation of
what a program is to do is called "pseudocode" and is one of the most valuable
tools available to a designer -- beginner or seasoned expert alike!) ( let
me repeat myself -- one of THE MOST VALUABLE TOOLS!! Different software
engineers use different styles of pseudocode, but pretty much any experienced
one of them could understand any style.):
While (laundry still on the line)
{
remove item from the line;
{ if (to be folded)
{process folding-routine}
elseif (to be ironed)
{process ironing routine}
else
{process hanging routine}
}
}
While (laundry still in the drier)
{
remove item from the drier;
{ if (to be folded)
{process folding-routine}
elseif (to be ironed)
{process ironing routine}
else
{process hanging routine}
}
}
{
remove item from the line;
{ if (to be folded)
{process folding-routine}
elseif (to be ironed)
{process ironing routine}
else
{process hanging routine}
}
}
While (laundry still in the drier)
{
remove item from the drier;
{ if (to be folded)
{process folding-routine}
elseif (to be ironed)
{process ironing routine}
else
{process hanging routine}
}
}
This isn't exactly PHP code, but it sure represents the way it can be coded!
(By the way, again, a seasoned programmer would instantly see that this routine
can be consolidated once again -- do you see it?) The interesting point
for our purpose in this tutorial is that it illustrates what PHP calls a
"function". A function is a routine (or more accurately, a sub-routine)
that can be called to perform its duties from any point in the program that
needs it. Here's the nitty-gritty of using a function in PHP:
Defining a Function
No rocket science here! -- Simply tell PHP we're talking about a function,
give it a name, follow it with ( ) (that's the part the gets closer to rocket
science -- more on that later) and specify the instructions it contains.
Like this:
function HangLaundry( )
{ instructions go here, between the braces }
{ instructions go here, between the braces }
Here we've defined a function called "HangLaundry". Function names are
case sensitive, meaning that HangLaundry is not the same as hanglaundry.
Personally, I like to capitalize each word in the name so that it is easier to
read (remember the part about being your own best friend?) The
instructions contained within a function are not processed (executed) until the
function is called, regardless or where within your program the function is
defined. This means that theoretically, your functions could be defined
anywhere within the program. Best practice, however, is to define all your
functions at the beginning of your program script. Again, you'll thank
yourself later.
Calling a Function
A function is called by its name followed by a set of parentheses, like this:
if ($process = = "hang")
{
HangLaundry ( )
}
{
HangLaundry ( )
}
This codes checks the value of a variable called $process and if it contains
"hang", calls the HangLaundry function.
Variable Scope and Lifetime
It's important to note that if you define a variable within a function, that
variable is only available within that function; it cannot be referenced in
another function or in the main body of your program code. This is known
as a variable's scope. The scope of a variable defined within a function
is confined to that function -- in other words, it's local to that function.
If a function needs to use a variable that is defined in the main body of the
program, it must reference it using the "global keyword, like this:
function AddingNumbers ( )
{
global $sum = 2 + 2
}
{
global $sum = 2 + 2
}
$sum = 0
Addnumbers ( )
print "2 + 2 = ".$sum
Addnumbers ( )
print "2 + 2 = ".$sum
While the scope of a variable defined in a function is local to that
function, a variable defined in the main body of code has a global scope.
The "global" keyword tells PHP to look for a variable that has been defined
outside the function.
There is also the idea of a lifetime for a variable. A variable defined
within a PHP program script exists only while that script is running. When
the script ends, the variable ceases to exist. This seems pretty obvious!
Now apply it to a function: a variable defined within a function exists
only while that function is being processed; when the function ends, the
variable ceases to exist. Ooooh! Aaaah- yes!!
There are also other ways to pass information back and forth between the main
body of code and the functions it calls (and also, since a function can also be
called from within another function, between one function and another.) I
told you we'd talk about that set of parentheses later -- we'll talk about them
next!
No comments:
Post a Comment
Thanks for comment me