Functions In PHP-User Defined

Function is a piece of code that is meant to perform some particular operation. This function can be executed any number of times. PHP also has many pre-defined functions but in this article, we are going to discuss on user-defined function.


Features and Properties of Functions:

  1. Modularity: Function brings in the concept of modularity in programs i.e. a problem can be broken down into sub-problems and those subproblems can be solved individually.
  2. Code Reusability: Function eliminates the need of writing the code again and again. Whenever we need the same code for execution we can just call the function again.
  3. Enhances Readability and Understanding: It provides a better understanding of the program. As it represents individual components hence it is better to analyze the logic behind the program.

User-defined functions are those functions that are declared and defined by the user/programmer.

Syntax:

function function_name(parameters)
{
//function body
}

function is pre-defined key word in PHP. The parameters specified inside the paranthesis are called the formal parameters.

Look at the following example for better understanding:

<?php  
function display($value)
{  
echo "$value";  
}  
display("Welcome to");  
display("Padhle.com");  
display("!!!");  
?>

The output will be as follows:

Welcome to Padhle.com!!!

Function Call

Function calling is done by simply using the function name along with the parameters. A function can be called from another function also. A function can also call itself. In the above code, you can observe that a function call has been made from the global scope of the program.


TYPES OF PARAMETERS

As mentioned earlier in the previous section that parameters in function definition are called formal parameters whereas parameters that are used during the function call are called actual parameters.


TYPES OF FUNCTION CALLS

There are two types of function calls in PHP:


Call By Value:

In this mechanism, the values of the original parameters are passed to the formal parameters. in case we change the value of the formal parameters the changes would not be reflected in the original parameters.

Look at the following example for understanding:

<?php  
function display($value)
{  
echo "$value";  
}  
display("Welcome to");  
display("Padhle.com");  
display("!!!");  
?>

The values of the strings have been passed to the formal parameter “$value” in the above program.


Call By Reference

In this mechanism, the addresses/references of the original parameters are passed to the formal parameters. in case we change the value of the formal parameters the changes will be reflected in the original parameters.

Look at the following example for understanding:

<?php  
function display($&value)
{  
echo "".$value; 
$value=$value+2; 
}  
$x=1; 
display($&x);
echo "".$x;
?>

You can see that the changes have been reflected in the original parameters also, see the output:

1
3

Returning Values

A value can also be returned from a function by using the return statement. Look at the following example:

<?php  
function display($value)
{  
echo "$value";
$value=$value+1; 
return $value 
}  
$x=1;
echo "".$x;
$x=display($x); 
echo "".$x; 
?>

The output of the program will be:

1
1
2

Default Arguments

The use of this concept assigns a default value to the parameters of the function which is used when the value is not received from the calling function. Look at the following example for better understanding:

<?php  
function display($value=10)
{  
echo "$value";  
}  
$x=1;
display($x);
display(); 
?>

The output of the above program will be :

1
10

If you have any doubts and queries, leave it in the comment section given below.

Leave a Comment

Your email address will not be published. Required fields are marked *