Functions in PHP
Functions are the robots of any programming languages and in PHP they just the same. They are mainly used to make code reusable and easy to read. If you need to record a show on television only one time, do you program the VCR? however, if you need to record the same show everday, you use the timer
and the programmer on the VCR to make it happen.
Use of Functions
You have been using functions all along in your PHP programming without actually calling them functions. So instead of having to enter the code over and over again, you just made a simple function call to the already defined function and one you went merrily.
The most important question to ask yourself when you are thinking about writing a function, is how many times am i gonna be using this same or reasonable same programming code to accomplish this task? Of if you see yourself having to use the code over and over again, then you need to put it in a function.
Example of a PreDefined Function
print( “I am a predefined function. yes i meaning PRINT”);
See now, I told you that you used functions all along. Behind the scenes there is code that tells print what to do with the information is it sent i.e, What information you want to have printed.
Syntax of Functions
A function, in simple terms takes input(argument(s)) and produces output(results(s)). The code within a function does operations on the inputs and then produces output.
function my_function ($agruement1, $agruement2, $agruement3)
{
return $fctval;
}
The above function, takes three parameters, and then returns a value. These parameters are passed by value,. Remember that for the future
function my_other_function ()
{
/* any valid php code. */
}
The above function, just does operations with no parameters, this can be used in many ways including writing output to the screen.
$returnedvalue = my_function($value1, $value2, $value3);
Since my_function returns a value, we can use it in any arithmetic function, just like we would use any other variable.
my_other_function();
Now that is just a basic introductions to functions, we will discuss functions in more details in a future tutorial.


