Michael Desmadril
Michael Desmadril

Reputation: 55

Return in a function - PHP

even if i do understand what everything is doing,I can't seem to find the logic in what return $totaal does. Below that working code:

  <?php

  function Adding ($getal1, $getal2){
    $totaal = $getal1 + $getal2;
    return $totaal;

  }

  function Substract ($getal1, $getal2){
    $totaal = $getal1 - $getal2; 
    return $totaal;
  }

  $getal1=10;
  $getal2=2;
  echo Adding ($getal1, $getal2) . "<br>";
  echo Substract ($getal1, $getal2);

  ?>

I make my function and i call it later by echo'ing it but what does that Return $totaal do in the function. I never call it, but without that return i get blanks.

I must be missing some kind of logic in my brains....

Upvotes: 0

Views: 3970

Answers (11)

Michael Desmadril
Michael Desmadril

Reputation: 55

The Return will only return the value once the function is called so the string: "Welcome Mr." together with the variable $name. That variable is going to be given a value when I call my function ("Piere"). Default, if I leave the field blank, I called my variable unknown, otherwise an error occurs. So:

    function naming ($name="Unknown"){
       return "Welcome Mr. $name";
    }

echo naming ("Pierre");

Upvotes: 1

Raghu Goriya
Raghu Goriya

Reputation: 88

you should store in variables and then you may check in if condition...

<?php
  $add = Adding ($getal1, $getal2);
  $sub = Substract ($getal1, $getal2);

  if($add!=""){
     echo $add."<br>";
  }
  if($sub!=""){
     echo $sub;
  }
?>

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31739

You could probably understand what return does where a function will produce some value and you will need it to be some where else.return statement stops the execution and goes to the calling line. like -

function hello() {
   return "welcome"; //return will send the produced value as a outcome of the function
}

$result = hello();// store the value returned by the function

Now $result will have the value.

Upvotes: 1

user2807217
user2807217

Reputation: 41

You can print (echo) result of the function - but more... for example other math:

<?php $multiplication = Adding(5,10) * Substract(20,10); ?>

Upvotes: 0

php_coder_3809625
php_coder_3809625

Reputation: 193

Return sets the function as a string, int, bool etc.

Without the key word return, the function is a blackhole.

In function Adding:

$totaal = $getal1 + $getal2;
return $totaal; // return returns $getal1+getal2

It can be shortened into:

return $getal1 + getal2;

When you are doing echo Adding($getal1, $getal2);

You are actually doing echo $getal1 + $getal2;

Upvotes: 2

Octfx
Octfx

Reputation: 699

return as the word itself says returns (gives back) something.
In this case either $var1 + $var2 or $var1 - $var2.

Variables inside of a function can't usually be accessed outside of it.
With return however you can get something from inside a function to use it oustide of it.

Keep in mind, that a return will end the execution of a function.

Upvotes: 7

Dorvalla
Dorvalla

Reputation: 5240

You need to bind the result of your function to a variable and echo that out. As a return wont show the actual data. Either that or you can echo it out within the function.

So it would be something like

function Adding($getal1,$getal2){
    $totaal = $getal1 + $getal2;
    return $totaal;
}

function Substract($getal1,$getal2){
    $totaal = $getal1 - $getal2; 
    return $totaal;
}

$getal1=10;
$getal2=2;
result1 = Adding($getal1,$getal2);
result2 = Substract($getal1 $getal2);

echo $result1." </br>";
echo $result2; 

Upvotes: 0

deviloper
deviloper

Reputation: 7240

When you call a function in a programming language, this means that you have some expetations from that function, as you are feeding it with some arguments(parameters) in your case: $getal1 and $getal2 the function in return will give you back, a value deriven by specific operations on your parameters.

each time you call a function you actually are asking it for responses

echo Adding ($getal1, $getal2); is actually the same as echo 12 but you use the function to do the same operation on dynamic data.

Upvotes: 0

Ali
Ali

Reputation: 3461

Return means that whatever value is returned, you can save it in a variable or use it by concatenation, I'll provide two examples, one with return and one without

With return (note: when return is called, it stops the execution of a function after returning the value):

Function AddFive($number) {
    Return $number += 5;
}
$number = AddFive(10); 
// $number now holds the value of 15 incase I need to manupilate it or do more stuff
Echo $number;

Without return:

Function AddFive($number) {
    Echo $number += 5;
}
AddFive(10); // this will echo out 15 immediately and I cannot manupilate it

It is always advisable to use return with functions if you really need it because that way you're seperating logic and presentation. However, some functions you may soon write such as ones that affect databases don't really need to return all values, meaning, use return if you need to echo out the value or manupilate it, don't use it if it does something that you don't need to manupilate

Upvotes: 0

geoandri
geoandri

Reputation: 2428

return defines the returning value of your function. Your functions use two parameters and return a value. The first one returns the sum of two parameters and the second one subtract their values. For example

$a = Adding(3,2) // the value assigned to $a will be 5
$b = Substract(10,8) // the value assigned to $a will be 2

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

The return statement immediately ends execution of the current function, and returns its argument as the value of the function call.

Without return you can echo it there itself like

function Adding ($getal1, $getal2){
    $totaal = $getal1 + $getal2;
    echo $totaal."<br>";
}
function Substract ($getal1, $getal2){
    $totaal = $getal1 - $getal2; 
    echo $totaal;
}

$getal1=10;
$getal2=2;
Adding ($getal1, $getal2);
Substract ($getal1, $getal2);

Upvotes: 1

Related Questions