Function signature

Function pow, that raises a number to a power, receives two paramters: a number to raise and a power to raise to. If we called pow without parameters, code execution would exit with an error PHP Warning: pow() expects exactly 2 parameters, 0 given in php shell code on line 1. This is the interpreter telling you that the function expects two parameters and you’ve passed it none.

Whoever created the pow function, designed it in the way that it requires two parameters, and it’s impossible to call it with any other number of parameters.

Besides, only numbers can be acceptable parameters for the pow function. Passing it a string, for example, would cause the following error: PHP Warning: A non-numeric value encountered in php shell code on line 1. Result of this function must also be a number.

Some other function can have a different number and type of parameters. There could be a function that receives three parameters: a number, a string, and another number.

How do we know how many parameters the pow function needs and what type of result it will return? We’ve looked up the signature of this function. Signature defines the input values and their types and also output value and it’s type. Let’s look up the pow function in the PHP official documentation. You will find the following text in the “Description” section:

pow ( number $base , number $exp ) : number

Returns base raised to the power of exp.

It is the function signature and a short description of it.

Let’s break it down:

  • the function’s name is pow
  • the function receives two parameters: a number (base) and a number (exp)
  • the function returns a number
  • function returns base raised to the power of exp.

You can only pass parameters in exactly the same order as it is defined in the function signature.

Any function returns only one value. Such limitaion exists on the language level and can’t be bypassed.

instructions

Now it’s your turn to look up a function signature in documentation and find out how to use it.

PHP has a function named ucfirst. Study it’s signature at the page http://php.net/manual/en/function.ucfirst.php.

Write a program that uses the ucfirst function on the variable $text and print the result to screen.


definitions

  • Function signature — a formal description of types of arguments and return value of a function.


Exercise available only for signed users.

Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on Github.