Skip to content
Theory · First program · lesson 5 of 7

Your first function

So far, the programs have printed ready-made text and numbers. Back to the recipe cards. In a cake recipe, the cream is described on a separate card, and the main recipe does not repeat its steps but says: "make the cream from the cream card".

The cook puts the main card aside, makes the cream from the second one and comes back to where they stopped, with the cream ready. That is how a function works in a program.

A function is steps written once under a name of their own. When the same calculation is needed in several places, it goes into a function. The function double takes a number and returns it doubled:

PHP
<?php

function double($x)
{
    return $x * 2;
}

echo double(3), "\n"; // 6
echo double(10), "\n"; // 20

double(3) is a function call. Let's follow the first call:

  1. double(3) sends execution into the function double, where $x is 3.
  2. return $x * 2; computes 6 and returns that number to where the call stood.
  3. echo gets 6 and prints it.

The second call works the same way with the number 10 and prints 20.

Declaring a function

Let's take the function double apart.

The function line

The line function double($x) starts the function declaration:

  • function is a keyword: "I am declaring a function";
  • double is the function's name, the one you call it by;
  • ($x) is a parameter in parentheses.

The body of the function is the statements in curly braces. By the coding style, a function's opening curly brace goes on a line of its own, and the body is moved 4 spaces to the right. No semicolon follows the closing brace: the function declaration ends with the brace itself.

Parameters and arguments

A parameter is a name that receives a value on every call. In PHP such names start with the $ sign, for example $x. In the call double(3), the value 3 is an argument, and it goes into the parameter $x.

Interviewers often ask beginners how a parameter differs from an argument:

  • a parameter is a name in the function declaration, for example $x;
  • an argument is a value passed in the call, for example 3.

The word return

return ends the function and returns the value written after it: here, $x * 2. The call itself is replaced with that value: double(3) in the line with echo turns into 6.

The function double prints nothing itself. The printing is done by echo, which holds the call.

Where the result goes

A call is a jump. PHP puts the current line aside, runs the body of the function and comes back to the same place with the result. The result can be kept under a name. The name $a, like $x, holds a value: $a = double(3); puts 6 into it, and echo $a prints what it holds.

Press "Next step" and watch the frames. The {main} frame is the program itself, and the double frame appears for the time of the call and disappears when return hands back the result.

LiveA function call: there and backstep 1 of 9 · frames: 1

PHP knows the function double before the run, but its body does not run yet.

The highlighted line runs now, and each frame holds the values of the program or of one call

The program printed 6 and 13. In the second call, double($a) returned 12, and only then did PHP add 1 to the result. Each call gets its own $x: in the first it is 3, in the second 6.

Here the card picture simplifies things. The cream from one card comes out the same every time, while a function gets its own argument on every call, and the result depends on it.

A call inside a call

An argument can be another call. Then the inner call runs first, and its result becomes the argument of the outer one:

PHP
echo double(double(2)), "\n"; // 8

Let's go step by step:

  1. The inner call double(2) returns 4.
  2. The outer call gets the argument 4 and returns 8.
  3. echo prints 8.

A call above the declaration

As you already know, PHP reads the whole file before the run. While doing so, it remembers the functions declared in the file, so a call may stand above the declaration:

PHP
<?php

echo triple(4), "\n"; // 12

function triple($x)
{
    return $x * 3;
}

PHP prints 12, although the function triple is declared below the line with the call.

Common mistake

The function is missing its return:

PHP
<?php

function double($x)
{
    $x * 2; // ✗
}

echo double(3), "\n";

There is no error message, but the program prints an empty line. The function computes $x * 2 and discards the result, and a call without return returns null, an empty value that echo prints as nothing. The fix is to write return $x * 2;, as in the first example.

Key points

  • A function is declared with the word function: a name, parameters in parentheses, a body in curly braces.
  • A parameter is a name in the function declaration, an argument is a value in the call.
  • A call goes into the body of the function, and return hands the value back to the place of the call.
  • In a nested call, the inner call runs first, and its result becomes the argument of the outer one.