Sunday 9 June 2013

USER-DEFINED FUNCTIONS IN PHP

Functions are small separate subprograms that are used to define a separate task that you wish to perform in your program many times, in a distributive program. For creating user defined function in php you need to insert function key word at the beginning of your function definition.
In this post I will be covering the following topics:-

  1. Creating a simple function to display a message.
  2. Creating a function that takes arguments.
  3. Concept of return statement. Returning values from a function.
  4. How to return multiple values from a single function.
  5. Concept of default arguments.

Consider the following source code:-
/*******************************************************************************/
<html>
                <head>
                                <title>Simple Function</title>
                </head>
                <body>
                                <?php
                                                function display_message1()
                                                {
                                                                echo "Hello World<br />";
                                                }
                                               
                                                display_message1();
                                ?>
                </body>

</html>
/********************************************************************************/
Output:-
Hello World


Here display_message1() is a function that is used to print a message.

Now after considering simple functions, let's move on to some advanced functions that can receive parameters as arguments.
/********************************************************************************/
<html>
                <head>
                                <title>Function taking argument</title>
                </head>
                <body>
                                <?php
                                                function display_message1($name)
                                                {
                                                                echo "Hello {$name}<br />";
                                                }
                                               
                                                display_message1("Rakshit");
                                                $str="Lavish";
                                                display_message1($str);
                                               
                                ?>
                </body>
</html>
/********************************************************************************/
Output:-
Hello Rakshit
Hello Lavish

After passing string parameters, now let us consider some return statements from the function. Consider the following program that takes two values as arguments and then returns sum of those two values.
Source Code
/****************************************************************************/
<html>
                <head>
                                <title>Returning a value</title>
                </head>
                <body>
                                <?php
                                                function add($a,$b)
                                                {
                                                                return ($a+$b);
                                                }
                                               
                                                echo add(5,3);
                                ?>
                </body>
</html>
/*****************************************************************************/
Output:-
8


Now, suppose you wish to return multiple values from a function, for this you can build an array and store all the values in that array and then return the array itself, instead off returning single values. Note that a function can have just one return statement.
Consider the following program in which you wish to construct a function that returns 4 different values after performing 4 basic operations, addition, subtraction, multiplication and division, of the two arguments that it takes.
/*******************************************************************************/
<html>
                <head>
                                <title>Returning multiple values</title>
                </head>
                <body>
                                <?php  
                                /* returning multiple values from a single function.... */
                                                function add_sub($a,$b)
                                                {
                                                                $sum=$a+$b;
                                                                $diff=$a-$b;
                                                                $product=$a*$b;
                                                                $quotient=$a/$b;
                                                                $arr=array("Result of addition = "=>$sum,
                                                                                   "Result of subtraction = "=>$diff,
                                                                                   "Result of multiplication = "=>$product,
                                                                                   "Result of division = "=>$quotient);
                                                                return $arr;
                                                }
                                               
                                                $var1=15;
                                                $var2=19;
                                                echo"var1 = ".$var1."<br />var2 = ".$var2;
                                                $arr=add_sub($var1,$var2);
                                                echo"<pre>";
                                                print_r($arr);
                                                echo"</pre>";
                                ?>
                </body>
</html>
/******************************************************************************/
Output:-
var1 = 15
var2 = 19
Array
(
    [Sum = ] => 34
    [Difference = ] => -4
    [product = ] => 285
    [quotient = ] => 0.78947368421053
)


Now the last thing that I would like to tell you about functions is regarding the concept of default parameters. Suppose if you have not specified sufficient number of arguments during a function call, then you will definitely get an error, but if you have supplied default parameters, then you will not get any error and the default arguments will be taken in place of the missing arguments at the time of function call.
Source Code:-
/********************************************************************************/
<html>
                <head>
                                <title>break</title>
                </head>
                <body>
                                <?php  
                                                function display_msg1($str="World")
                                                {
                                                                echo"Hello {$str}"."<br />";
                                               
                                                }
                                               
                                                display_msg1();
                                                display_msg1("Lavish");
                                                display_msg1("Mahaveer");
                               
                                                function display_msg2($str1="How",$str2="Where",$name="Durga")
                                                {
                                                                echo"{$str1} are you...{$name}?<br />";
                                                                echo"{$str2} are you...{$name}?<br /><br />";
                                                }
                                               
                                                display_msg2("Who","how","Rohit");
                                                display_msg2();
                                                display_msg2("a","b");
                                                display_msg2("hii..");
                                ?>
                </body>

</html>
/*******************************************************************************/
Output:-
Hello World
Hello Lavish
Hello Mahaveer
Who are you...Rohit?
how are you...Rohit?

How are you...Durga?
Where are you...Durga?

a are you...Durga?
b are you...Durga?

hii.. are you...Durga?
Where are you...Durga?

No comments:

Post a Comment