Home

Introruction to Functions

Functions Fundamentals

Introduction to Functions

A function is a section of code that may performs an action to produce a value or a result.

Creating a Function

The fundamental formula used to create a function is:

function function-name
{
    function body
}

You start with the function keyword, followed by a name for the function. The name follows the rules of names of variables without the $ symbol. By tradition, the name of a function conveys an action.

Defining a Function

Defining or implementing a function is equivalent to specifying its purpose. The definition or implementation of a function is done in its body, which is between the curly brackets. Normally, you can do anything you want in the body of a function. If you create expressions or write statements, each must end with a semicolon. For example, if necessary, you can declare one or more variables and use them as you see fit. The variables declared in a function are referred to as local variables. Here are examples of local variables:

<?php
function display()
{
    $sentence = "We need to talk!";
    $number = 24;
    $result = "About what?";
}
?>

You can also use echo in a function to display something. Here is an example:

<?php
function display()
{
    echo "We need to talk!";
}
?>

Calling a Function

After creating a function, to see its result, you must call it. To do this, in the position where you want to use the action of the function, type its name followed by parentheses. Here is an example:

<?php
function display()
{
    echo "We need to talk!";
}

// . . .

display()
?>

This would produce:

Introduction to Functions

The Returned Value of a Function

The return value of a function is the value the function produces after performing its action(s). To specify the return value of a function, write the return keyword followed by the desired value. The return value can be the name of a variable. Here is an example:

<?php
function display()
{
    $message = "Welcome to the wonderful world of PHP!";
    return $message;
}
?>

If the variable is not used many times, or you are not planning to use the variable more than once, you can give it directly to the return keyword. Here is an example:

<?php
function display()
{
    return "Welcome to the wonderful world of PHP!";
}
?>

To get a value returned by a function, when calling the function, you can assign its call to a variable to store the value in the variable. Here is an example:

<?php
function display()
{
    return "Welcome to the wonderful world of PHP!";
}

$msg = display();
?>

You can use such a variable like any other. For example, you can display its value using echo. Here is an example:

<?php
function display()
{
    return "Welcome to the wonderful world of PHP!";
}

$msg = display();

echo "Message: ";
echo $msg"
?>

This would produce:

The Returned Value of a Function

If you are not planning to use the variable many times, or you are not planning to call the function many times, you can just call the function directly where its value is needed. Here is an example:

<?php
function display()
{
    return "Welcome to the wonderful world of PHP!";
}

echo display();
?>

This would produce:

The Returned Value of a Function

From past lessons, remember that you can include variable in the double-quotes of echo. If you call a function directly in double-quotes, it will not be called.

Parameters and Arguments

Introduction

To perform its actions, a function may need some values. These are called parameters and they must be supplied to the function. The formula to create such a function is:

function function-name(parameter(s))
{
    function body
}

In this case, in the parentheses of the function, add the desired parameters. Each parameter is represented by a name preeceded with the $ symbol. Here is an example:

<?php
function calculate($number)
{
    . . .
}
?>

If the function must use more than one parameter, in the parentheses, specify a name for each parameter. The parameters are separated by commas. Here is an example:

<?php
function add($a, $b)
{
}
?>

To define or implement the function, in the body section, use the parameter as you see fit. Here is an example:

<?php
function calculate($number)
{
    $number * $number;
}
?>

In the same way, if the function uses more than one parameter, you can use any of them in the body of the function.

Calling a Function With Parameters

To call a function that takes (a) parameter(s), you must supply a value for the parameter(s). This is referred to as passing the argument(s) to the function. To do this, when calling the function, in its parentheses, provide the (list of) argument(s). Here is an example:

<?php
function calculate($number)
{
    $number * $number;
}

calculate(24.50)
?>

(This code will not produce anything)

As seen above, to.specify the return value of a function, write the return keyword followed by the desired value. The return value can be the name of a variable. Here is an example:

<?php
function calculate($number)
{
    $square = $number * $number;
    return $square;
}
?>

If the variable is not used many times, you can give it directly to the return keyword. Here are examples:

<?php
function calculate($number)
{
    return $number * $number;
}
?>

When calling the function, provide the value in the parentheses and you can assign it to a variable. Here is an example:

<?php
function calculate($number)
{
    return $number * $number;
}

$calculation = calculate(24.50);
?>

You can then use the variable. Here is an example:

<?php
function calculate($number)
{
    return $number * $number;
}

$calculation = calculate(24.50);

echo "Result: ";
echo $calculation;
?>

This would produce:

Introduction to Functions

Remember that you can put the variable directly in the double-quotes of echo. Here is an example:

<?php
function calculate($number)
{
    return $number * $number;
}

$calculation = calculate(24.50);

echo "Result: $calculation";
;
?>

If the function is taking more than one argument, when calling it, in the parentheses, type the value of each parameter. The values are separated by commas. Here is an example:

<?php
function add($a, $b)
{
    return $a + $b;
}

$addition = add(24.50, 38.67);

echo "Result: $addition";
?>

This would produce:

A Function With Parameters

Introduction to Built-In Functions

Introduction

A function is referred to as built-in if it is already created and you can just use it directly in your code. PHP provides many ready-made functions.


Previous Copyright © 2015-2016 FunctionX Next