0

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....

1
  • echo construct needs an argument to perform its work. That argument is the value returned by the function.Think of it like this -First addition is done, then value is returned, and then echo uses that value to print it. Commented Oct 28, 2014 at 10:20

11 Answers 11

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I understand this but the thing that is confusing me is that i never use $totaal outside of the function. I don't call it anywhere...its there in the function, so $getal1 + $getal2 is stored in $totaal. But where in my example do i access my $totaal from outside my function?
@MichaelDesmadril I think you want it like this: $result = Adding($getal1, $getal2). Then you have the result stored outside the function.
Yes, good advice but i'm trying to find out where $totaal is going? Or shouldn't i be using that? And use the short version as some people here suggested?
2

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;

Comments

1

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);

Comments

1

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.

Comments

1

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");

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

0

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; 

Comments

0

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

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

Comments

0

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;
  }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.