PHP: Functions I
Derek Bridge
Department of Computer Science,
University College Cork
PHP: Functions I
Aims:
- to revise what we know about functions, especially built-in functions
- to learn how to define a simple function
- to learn the details of invoking a function
Revision of functions
- In PHP, a function is a named block of PHP that performs some task
- In PHP documentation, functions are described by giving
- their prototype, which specifies
- the return type
- the name of the function
- the type of each parameter
- some sentences of explanation
- E.g., the entry in the PHP manual for the
sqrt
function
Defining functions versus invoking functions
- Before it can be used, a function must be defined
(or declared)
- i.e. write a block of PHP and give it a name
- Once this has been done, the function can be invoked
(or called, or executed, or run)
- i.e. use it in your own programs, e.g.
sqrt(9)
- PHP has many built-in functions
- pre-defined (already written)
- for you to invoke (use) wherever you wish
- e.g.
date
, sqrt
, abs
, pow
,
floor
, ceil
, round
,
count
, array_sum
, in_array
,
min
, max
,
explode
, trim
, strlen
,
array_key_exists
, isset
, array_keys
,
array_values
,…
- But the programmer can define his/her own functions (programmer-defined, or
'user-defined')
Example: defining a function for computing $x2
function square( $x )
{
return $x * $x;
}
Observations
- Use the keyword
function
- Then give the name of the function:
- a mixture of letters, digits and underscores but not starting with
a digit (and case-insensitive)
- different from the name of any existing function (whether built-in or
programmer-defined)
- Advice: choose a meaningful name that describes the purpose of
your function
- Advice: if it is hard to choose a concise name to describe your
function's purpose, then maybe yours is an ill-advised function
- Next indicate the parameters (more on this later)
- Finally, give the body:
a block of code (i.e. zero, one or more statements in braces)
Observations
- Defining a function simply associates the name with the body (block of code)
- The body is not actually executed until you invoke the function
Class exercise: what is the output of the following?
<?php
function square( $x )
{
return $x * $x;
}
echo '<table>';
$y = square(1);
echo '<tr><td>1</td><td>' . $y . '</td></tr>';
echo '<tr><td>2</td><td>' . square(2) . '</td></tr>';
echo '<tr><td>3</td><td>' . square(2 + 1) . '</td></tr>';
echo '<tr><td>4</td><td>' . square( square(2) ) . '</td></tr>';
$z = 5;
echo '<tr><td>5</td><td>' . square($z) . '</td></tr>';
echo '</table>';
?>
Formal parameters and actual parameters
- On the previous slide, distinguish between
- the formal parameters, which appear in the function definition
(in this case just
$x
)
- the actual parameters, which appear in function invocation
(e.g.
2
)
- The formal parameters are just variables
- when the function is invoked, these variables are created and
they are initialised using the actual parameters
- these variables are used within the body of the function
- they are destroyed when execution of the body of the function ends
- In the simplest case, a function invocation needs as many actual parameters
as there are formal parameters, and in the right order
- The actual parameters should be of the right type (although PHP will
do its usual type conversions if they aren't)
- Alternative terminology: formal arguments, actual arguments
Class exercise
- Define a function that returns the volume of a cylinder
- From geometry, if a cylinder has radius r and height h, then
its volume V is given by
V = πr2h
- Use 3.14 for π
- Volume of a cylinder
- Write statements that invoke your function to compute and echo the volume
of a cylinder whose radius is 10 and whose height is 20, and a cylinder
whose radius is 20 and whose height is 10
The return
statement
- When a function is invoked, the function body is executed
- Execution of the function body terminates
- when the closing brace of the function body is reached, or
- when a
return
statement is executed
- At this point, execution of the code that did the invoking is resumed
Pointers for advanced students
- When you invoke a function, if you supply fewer actual
parameters than there are formal parameters,
these parameters will remain unset (and a warning will be issued)
- But when you define a function, it is possible for you to supply
a default value for a parameter, which will be used
if the actual parameter is omitted when the function is invoked
- It is even possible to define functions that accept a variable
number of parameters
- It is possible to store functions in variables
- It is possible to pass functions as parameters into other
functions
- It is possible to define anonymous functions, especially
when you want to pass these into other functions