Functions and Parameters
All of these languages provide many built-in functions, and if you need a job performed you should always check to see if a built-in function is available for the task. This page provides an overview of functions and parameters in the different languages in case you do need to define your own, and so that you will have a better idea of how the built-in ones work.
Function definitions are generally placed in the head of an (X)HTML page, and calls to such a function normally appear in the body of that page. A function definition looks like this:
function name([par1, par2, ...]) { statements }A function is called by using its name, with an actual parameter list, if it has one, in parentheses. A function return value is undefined unless explicitly returned.
Implicitly declared variables (even in functions) have global scope.
Variables declared with var inside a function are local to the function.
Local takes precedence over global.
Parameters are passed by value. There may be more or fewer actual parameters than formal parameters, in which case excess formal parameters are given a value of undefined, while excess actual parameters are ignored. In any case, the arguments array of a function contains all actual parameters passed when the function was called.
function name([par1, par2, ...]) { statements }Remember that function names are not case-sensitive in PHP. Nested functions are permitted, but discouraged. Return values must be explicitly returned.
Pass-by-value is the default parameter-passing mechanism. There may be more or fewer actual parameters than formal parameters, in which case excess formal parameters will be unbound, while excess actual parameters are ignored. In any case, the arguments array of a function contains all actual parameters passed when the function was called.
Pass-by-reference may be accomplished by placing an ampersand (&) in front of the formal parameter or the corresponding actual parameter.
The default scope of a variable declared in a function is local, but it can be made global by preceding its declaration with the global keyword.
The default lifetime of a local variable in a function is from first use to the end of the function call to the function in which it has been delcared. The static key word makes this lifetime extend from first use to the end of the full script.
sub name { statements }
Neither formal parameters nor a return type are a part of the definition. Return values can be explicit or implicit (value of last expression evaluated in the body of the function). Function definitions can appear anywhere in your script, but should be at the beginning or the end (in the latter case, with prior declarations).
A previously-declared function can be treated as a list operator (no parentheses required).
Every function has its own copy of the built-in array variable @_, and any actual parameters passed to a function when it's called are placed in this array and will be manipulated using pass-by-reference semantics (for pass-by-value semantics, copies of this array's contents must be made).