Monday, February 15, 2016

PHP FUNCTION


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)

No comments:

Post a Comment