Djnmarti.com



What Is a Function?

We have already seen a number of PHP built-in functions to perform tests on variables, display output, and more. PHP provides you with a large array of built-in functions to perform common tasks to save you the trouble of writing your own functions. As your programs become more sophisticated, you might find that the particular task you need is not handled by PHP or that you want to break your program into smaller units to make it more manageable. Then it is time to write your own functions, and this lesson will show you how. But first things first—let's define a function now.

Functions are self-contained units of a program designed to accomplish a specified task such as calculating a mortgage payment, retrieving data from a database, or checking for valid input. When a function is called in a program, it is like taking a detour from the main part of the program. PHP starts executing the instructions in the function, and when finished, returns to the main program and picks up where it left off. Functions can be used over and over again and thus save you from repetitious programming. They are also used to break up a program into smaller modules to keep it better organized and easier to maintain.

By definition, a function is a block of statements that not only performs some task, but can also return a value. A function is independent of your program and not executed until called. A function, often referred to as a "black box," is like the pocket calculator or remote control. Information goes into the black box as input (like the calculator or remote control when you push buttons), and the action or value returned from the box is its output (such as a calculation or a different channel). What goes on inside the box is transparent to the user. The programmer who writes the function is the only one who cares about those details. When you use PHP's built-in functions such as print() or rand(), you send a string of text or a number to the function, and it sends something back. You do not care how it does its job, you just expect it to work. If you send bad input, you get back bad output or maybe nothing, hence the expression "Garbage in, garbage out."

Functions are like miniscripts. They contain PHP statements that behave as a single command and can be called repeatedly throughout a program without rewriting the code.

The term function as used in this lesson is a stand-alone block of statements, independent of the program until invoked by a caller.

Function Declaration, Definition, and Invocation

The terms declaration and definition are often used interchangeably, but they are really different terms. To declare a function is to give it a name followed by the keyword function, and a set of parentheses that might or might not contain parameters (messages) that the function will accept.

function do_something() // Declaration

The function definition is the declaration and the body of statements found within the curly braces after the function's name.

function do_something(){

statements; // Definition

}

By itself, a function does not do anything. Invocation refers to calling a function; that is, telling PHP to start executing the statements within the curly braces. Typically, a function is called from a statement in another part of the program by its name, followed by a set of parentheses that might or might not be empty.

do_something();

Now we are ready to put all of these peices together to create a function and call it.

Where to Put Functions

User-defined functions can be declared anywhere within a file. They are placed within the tags and are available only to the script where they are defined. The function can be called before or after its definition because PHP internally compiles all function definitions before it executes any of the statements in the script.

Commonly used functions are often stored in separate files to reuse them as needed.

How to Define a Function

To define a function, the function keyword is followed by the name of the function, and a set of parentheses. The parentheses are used to hold parameters, values that are received by the function. The function's statements are enclosed in curly braces.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download