Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

What does "return" mean in a function

Hi there, I'm trying to hammer the basic concepts of php in my head. Regarding the word "return", can someone give me a quick explanation of what this means?

I know that if I were to run the following code, I need to place the word return. But I was wondering why?

function addNumbers ($arg1, $arg2)
{
     return $arg1 + $arg2;
}
print addNumbers ( 4, 5);

3 Answers

You can use return to immediately end execution of the current function. Let me give you example:

function addNumbers($x, $y) {
   if(!is_int($x) || !is_int($y)) {
       return 'You can only add numbers';
   }
   return $x + $y;
}

echo addNumbers('Hello','World');  // it will echo out this string: "You can only add numbers"
echo addNumbers(5,10);  // It will echo 15 because it successfully passed our if statement

In the above example we defined a function named addNumbers() which takes two arguments. In the functions declaration we first check if the both two arguments are not integers. If they are not then we use return to stop further execution of the function and we instead return a string which says "You can only add numbers". So if you insert integers for the arguments then it will skip through the if statement code and it will return the sum of the two arguments provided. Also remember that return does not print anything, it only returns data which you can also store in a variable. So now you can see how we can use return in the function.

hey big thanks everyone for helping me understand "return" a bit better. Bhawan, your example helped me learn some new stuff I haven't seen. I'm starting to get the hang of return. For me, I couldn't understand why even use the word "return" when you don't want to print anything. But now I'm seeing that you need to jump outside the function once to move forward. Thanks again!

Your welcome Michael :-) I'am glad that you learned something new

As I understand it, and it may not be the correct definition, return does as it suggests.

Basically, it means run this bit of code and return the result of the code so that we can do something with the result.

The definition of the return construct is "stop execution of the current function/script and return a value to the line that called it". It can be used anywhere in a function/script to stop execution and return flow.

The use of return is optional, in that if you do not call return explicitly then internally PHP will return NULL from a function.

In the OPs example the function addNumbers is returning the result of adding the 2 passed variables to the calling print statement.