Function Declaration and Usage



Function plays an important role in the programming world. For the discovery of function now it is easy to divide a large code and work with several sections with the help of function. Let's discuss different parts of a function

Function Declaration

For writing a function it is needed to use the word [php]function[/php]. And then the name of the function like below(you must need to write PHP code inside <?php ?> block) [php]function calculate[/php]. After that, we need to use first brackets that means parenthesis (in programming language first brackets are known as parenthesis). So let's give parenthesis like below [php]function calculate()[/php] .  Finally, it is time to implement body where a function stores and gathers data, process it, give or return the result. For implementing body part you need to use curly braces (in programming language second brackets are known as curly braces) like below [php]function calculate(){ } [/php]

Function Parameter and Argument

Function parameter and argument are used for sending and receiving data. After receiving data a function may use that or process it. After that function gives the output. For better clearance, it can go through a function declared below


<?php

function employeeManagement($name, $salary, $presence) { //function Declaration 
    echo "Name is " . $name . "
";     echo "Salary is " . $salary . "
";     echo "Total working " . $presence."days"; } ?>

What is Argument

If we notice the declared function employeeManagement then we see that there are three variables available inside a parenthesis. This parenthesis is called the argument of a function. Parenthesis receives value stores/process it and after the gives the output. A function may have an unlimited argument. But better practice is to use arguments not more than ten.

What is Parameter

A function is valueless until it is called from other places. For knowing more about function calling go  here. A parameter is a system of sending data by using variable/directly to a function. After sending data, function receives it, process it and finally give the wanted output. Let's see the below example for function calling to employeeManagement. 


<?php 

  employeeManagement('John Doe','5000$','27'); //function calling
 
  /*output of the function*/
  Name is : "John Doe"
  Salary is 5000$
  Total working 27 days

?>

Function Return Value

If we notice parameter section then we see that a function is receiving data, processing it and gives output. But it is not giving any value that can be used to another place. The concept of returning function value is

"when a function receives data, process it and return it to use in any place where it is needed then it is called function return value"

Let's see an example for better clearance


<?php

    function additionTwoNumber($firstNum, $secondNum) { //data receiving by argument
        $result = $firstNum + $secondNum; //addition process
        return $result; //returning value
     }

    $additionResult = additionTwoNumber(10, 20);
    echo $additionResult; //output is 30
    //now we will use this 30 to another place
    $thirdNum = 30;
    $value = $thirdNum + $additionResult;
    echo $value; //now result is 60
?

You can also use returning value to any place without storing it to a variable that means directly. See the example


<?php

    function additionTwoNumber($firstNum, $secondNum) { //data receiving by argument
        $result = $firstNum + $secondNum; //addition process
        return $result; //returning value
     }
    
    $value = $thirdNum + additionTwoNumber(10, 20);
    echo $value; //now result is 60
?

Hope that you have got a better idea about function declaration, usage, and its implementation. Stay with us. For knowing more comment......