Searching...
Thursday 4 October 2012

User Defined Functions

11:01 pm

User Defined Functions

User Defined Functions : 

1.       Function<function_name>(arg1,arg2,arg3,……..argn)
{
  Body of the function
}
2.       Any valid php code may appear inside a function. Function name follows the same rules as variable naming rules in php. A function will be executed by a call to the function.
3.       “Return  “ script used inside a function to return a vlaue back to the caller.
          <?php
 function writename()
 {
                                                 echo "<h1> Chandu</h1>";
 }
 ?>
 <?php
  writename();
  writename();
  ?>

Functions – passing and returning parameters:
             <?php
function addname($x,$y)
{
    $r= $x+$y;
    return $r;
}
?>
<h1>
    <?php
    echo "sum".addnum(11,22)."<br>";
    $c=  addname("India","Tech");
    echo "res=. $c;
    <?
</h1>
A parameter can be passed to a function using two techniques.
1.       Pass by value:
Here what ever changes happened to the parameter passed in a function will not gets reflected in the caller.
           <?php
           function change($x)
               {
                   $x=22;
   
                }
                    ?>
             <?php
              $x=10;
              echo $x."<br>";
               change($x);
                echo $x;
                 ?>
2.       Pass by reference:
Here what ever changes happens to the parameter pass the function will gets reflected in the caller.
<?php
function change($x)
{
    $x=22;
   
}
?>
<?php
$x=10;
echo $x."<br>";
change($x);
echo $x;
?>

0 comments:

Post a Comment