⊗ppPmUFSC 198 of 447 menu

Sequential Function Calls in PHP

The result of one function's work can be passed as a parameter to another. In the following example, we will first find the square of the number 2, and then the square of the result:

<?php function func($num) { return $num * $num; } $res = func(func(2)); echo $res; // outputs 16 ?>

The functions, of course, do not have to be the same.

Let's say, for example, we have a function that returns the square of a number, and a function that returns the cube of a number:

<?php function square($num) { return $num * $num; } function cube($num) { return $num * $num * $num; } ?>

Let's use these functions to square the number 2, and then cube the result of that operation:

<?php $res = cube(square(2)); echo $res; ?>

Now let's say we have a function that returns the square of a number, and a function that finds the sum of two numbers:

<?php function square($num) { return $num * $num; } function sum($num1, $num2) { return $num1 + $num2; } ?>

Let's use these functions to find the sum of the square of the number 2 and the square of the number 3:

<?php $res = sum(square(2), square(3)); echo $res; ?>

Suppose you have a function that returns the cubic root of a number, and a function that rounds a fraction to three decimal places:

<?php function root($num) { return pow($num, 1/3); } function norm($num) { return round($num, 3); } ?>

Using these functions, find the cubic root of the number 2 and round it to three decimal places.

byenru