Introduction to PHP – Part8

advertisement

advertisement

Function:

A function is a subprogram that performs a specific task when called by the main program. It is always a good practice to divide a huge program into a number of subprograms until elementary functions are reached. More clearly, a huge program should be divided into number of functions, and a function precisely should do one job. Suppose, we have a big program that reads data from user at the beginning, save data to a database and displays the saved data at the end. We can divide this program into three subprograms or functions. The first function reads data from user, the second function writes data to the database and finally third function writes data to monitor.

There are number of advantages of implementing a huge program into a number of subprograms. The first advantage is we can reuse the existing code. During a single execution of a program, a function can be called several times from different places and even from another function. The second advantage is it is easy to understand the program logic, debugging and testing is easier.

Functions can be categorised in two broad topics, built-in functions and user defined functions. Built-in functions are the language constructs that comes with PHP. There are hundreds of built-in functions available in PHP for our use. User-defined functions are the functions created by programmers. In this tutorial we will discuss user-defined functions in detail.

Creating function

Let us start with a simple function that calculates sum of two numbers.

Example#1

<html>
<body>
<?php
/*
This function calculates the sum of two numbers
*/
function sum()
{
            //declare and initialize variables
            $x=20;
            $y=30;
            $z=$x+$y;
            //print the result
            echo 'The value of $x is '.$x. ' and value of $y is '. $y .' The result of  $x + $y is '.$z;
}
//Execution of this program starts from here, and calls the function sum, that means code inside the function sum will be executed
sum();
?>
</body>

 
Function with parameter

Though we can call the function sum in example#1 as many times as we want, it produces the same result. Let us modify the above code to achieve better result.

Example#2

<html>
<body>
<?php
/*
This function calculates the sum of two numbers
*/
function sum($x,$y)
{
            //Calculate result
            $result=$x+$y;
            //print the result
            echo $result;
}
//Execution starts from here
$x=20;
$y=30;
sum($x,$y);

//We can call the function using different value
$x=200;
$y=300;
sum($x,$y);
?>
</body>

The code in both examples provides the same functionality but the variables in example#1 are hardcoded inside the function itself and hence, can provide the functionality of adding the hardcoded values. If we have to add other numbers we have to write another function. The code in example#2 solves this problem by passing parameters and provides more functionality. We can add any two numbers any times by just calling the function with parameters. The function sum in example#2 takes two parameters. The parameters are enclosed in parenthesis and separated by comma.

Function with parameter and return value

A function can also return value. The return statement is optional. A function can return any type including arrays and objects. After a function returns a value, its execution is passed back to the line from which it was called. Let us modify the code of example#2 to see how a function can return value.

Example#3

<html>
<body>
<?php
/*
This function calculates and returns the sum of two numbers
*/
function sum($x,$y)
{
            //Calculate result
            $result=$x+$y;
            return $result;
}
//Execution starts from here
$x=200;
$y=300;
//calculate and print the result
echo "The sum is : ". sum($x,$y);
?>
</body>

Don’t want to code your own website? You can hire a website development service like Act Now Domains to design your website and code it for you.

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.