PHP Function With arguments


What is function with arguments?

Functions are the method of performing tasks. Functions help you create functionality according to your task. In PHP, we can create own functions and pass the arguments. In the previous tutorials, we discussed the built-in functions and the user-defined functions . You can pass the arguments in built-in functions and pass arguments by creating own functions. Function with arguments means that you pass arguments in the function and define the value of arguments (parameter variables ) at the time of call.

 
function hello(arguments) {
block of code 
  }
 hello(values)

Definition of function with arguments

Function with arguments refers to a function that contains parameters(arguments) inside the parentheses. Function with arguments may be built-in functions or user-defined functions. In the function with arguments, we pass the arguments(variables) in function and define the value of variables at the end of the function the call.

Syntax

function function_name(arguments)
{
code of block/functionality
}
function_name(values)

Examples of function with arguments

We use the "Function" word to create the function. With the function word, we have to give the name of the function as well. We use parenthesis with the function name. Arguments (parameters, variables) are passed within the parentheses. In the example below, we create a function and pass the two variables as arguments. We call the function using function name only and define the values of the variables.

Example

<?php
function sum ($z,$y)
{

$sumvalue=$z+$y;
echo$sumvalue;	
	
}
sum(12,4);  
?>

Run

In the above example, we pass two variables as parameters in a function. We have some functionalities in the function block so that we can call the function's functionality. Out of the function block, we call the function using the name of the function. Along with calling the function, we define values as arguments to the variables.

Default argument values in PHP

In the previous tutorials, we discussed the pass by value. The arguments in the pass by value are effect locally.A function can define C ++ - style default values for scalar arguments. We pass arguments value in the function. This is what we call the default arguments values. In the following example, pass variables as parameters without specifying default argument value. According to this, it takes the default value as the argument. Follow the function with arguments example :-

Example

<?php
function product($category = "Mobile")
{
    echo "Buy a product $category.
"; } product(); product(null); product("Tablet"); ?>

Output


Buy a product Mobile.
Buy a product.
Buy a product Tablet.

PHP Passing arguments by reference

As we have discussed pass by reference in the previous tutorial. In pass by reference, arguments effects globally. According to PHP, by default, function arguments are pass by value. pass by value means the value of the argument can be changed within a function. Pass by value based on the local scope. You can allow the function to modify its arguments, they must be passed by reference as follows :

Example

<?php
function func_ref(&$product)
{
    $product.= 'along with buy a tablet.';
}
$prod= 'Buy a mobile, ';
func_ref($prod);
echo $prod; 
?>

Output


Buy a mobile, along with buy a tablet.

Built-In Functions


Please Share

Recommended Posts:-