Page Stats
Visitor: 409
A function is a block of statements that perform a task. A function can be call when required instead of writing that code again and again. When the function is call the control passes to that function, execute the function statements and return back from where it is called.
In PHP, Function names are NOT case-sensitive. A function name can start with a letter or underscore but not with a number.
Functions are of 2 types:
Pre-Defined Functions
User-Defined Funcitons
To create a User-Defined Function, we have to specify with function keyword followed by function_name, function parameters and function statements inside the curly braces.
Example 1: Print Hello World using Function.A function argument is a variable which is used to store the value passed to a function for furthur calculations. You can pass as many as parameters your like.
Example 2: Find sum of 2 numbers using Function.A function can return a value to the function call, return value is the result of the function calculation.
Example 3.1: Find sum of 2 numbers using function with return value.You can return more than one value from a function using return array(values).
Example 3.2: Find Sum, Subtraction, Multiplication, Division of 2 numbers using function with return value.In PHP, we can pass the value to the function parameter by using: value or reference. Reference is the alias to the variable name. Any changes made to an argument will change the value of the original variable. You can pass an argument by reference by adding an ampersand to the variable name in function definition.
Example 4: Function with argument passing by value and passing by reference.Default Argument are useful to provide default values in the function declaration. A function with default arguments can be called without specifying its arguments value.
Default argument must be declared from right to left.