Routines in PHP, using return

3

In the PHP course I'm doing, we're now in routines, and we started using return, but I still do not quite understand it.

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="_css/estilo.css"/>
    <meta charset="UTF-8"/>
    <title>Curso de PHP - CursoemVideo.com</title>
</head>
<body>
<div>

<?php
    function soma($a,$b){
        return($a + $b);
    } 
      $x = 3;
      $y = 8;
      $res = soma($x,$y);
      echo"A soma entre $x e $y é = $res";

?> 
<br/>
<!--<a href="javascript:history.go(-1)" class="botao">VOLTAR</a>-->
</div>
</body>
</html>

Why use it exactly? Was not it just creating an echo a + b and done? I do not understand the return function very well

    
asked by anonymous 25.05.2017 / 17:00

2 answers

3

For a simple example, like the data in the question, it does not really make such a difference between using return or echo directly in the result. However, with the complexity of your application increasing, you will understand more easily. Let's first consider the example using echo :

function soma($a, $b)
{
    echo $a + $b;
}

When doing:

$x = 3;
$y = 8;
$res = soma($x, $y);
echo "A soma entre $x e $y é = $res";

Your result would be:

11
A soma entre 3 e 8 é =

That is, the value of the result was displayed before. It was already expected, since the result of the sum is already displayed at the time of the function call. You may think: ok, but we can do the following:

$x = 3;
$y = 8;
echo "A soma entre $x e $y é = ", soma($x, $y);

The result will be:

A soma entre 3 e 8 é = 11

Resolved.

For this example, yes, but now use the soma function to calculate the following expression: 1 + 4 + 11. You can implement another function for this:

function soma3($a, $b, $c)
{
    echo $a + $b + $c;
}

$x = 1;
$y = 4;
$z = 11;
echo "A soma entre $x, $y e $z é = ", soma3($x, $y, $z);

The result, in fact, will be:

A soma entre 1, 4 e 11 é = 16 

But what if you need other expressions? For example, for 4 or 5 values, will you implement a function for each? Mathematically we know that to sum three values we can add the first two and the result to add with the third (associative property). That is, using the soma function, but now with return :

function soma($a, $b)
{
    return $a + $b;
}

To analyze the expression 1 + 4 + 11, we can do:

$x = 1;
$y = 4;
$z = 11;

$res1 = soma($x, $y); // Faz $res1 = $x + $y
$res2 = soma($res1, $z); // Faz $res2 = $res1 + $z

echo "A soma entre $x, $y e $z é = $res2";

That the result will be exactly:

A soma entre 1, 4 e 11 é = 16 

And if you need 4 values, type 2 + 3 + 5 + 7? No problems .

$x = 2;
$y = 3;
$z = 5;
$w = 7;

$res1 = soma($x, $y); // $res1 = $x + $y
$res2 = soma($z, $w); // $res2 = $z + $w
$res3 = soma($res1, $res2); // $res3 = $res1 + $res2;

echo "A soma entre $x, $y, $z e $w é = $res3";

And the result will be:

A soma entre 2, 3, 5 e 7 é = 17 

That is, with only one function we can parse multiple expressions. Using echo does not have this freedom (code reuse).

    
25.05.2017 / 17:23
3

The utility of return is to 'transfer' a value from within a function out of it. A function returns some value so that it is handled in other parts of the program / script or works as a confirmation whether the operation was successful or not.

  

Why use it exactly? Was not it just creating an echo a + b and done?

Depends on the case, now imagine that the result of soma() needs to be set to 10% outside function, echo $a + $b makes sense?

    
25.05.2017 / 17:16