What Does "return" Mean In A Function (Example) - Treehouse

Join the Treehouse affiliate program and earn 25% recurring commission!

✨ Earn college credits in Cybersecurity, JS, HTML, CSS and Python

  • Treehouse Logo
  • Plans Chevron
    • For Individuals
    • For Businesses
    • For Schools
    • For Libraries
    • For Military
  • Library Chevron
    • All Courses
    • Tracks
    • College Credit
  • Techdegree Chevron
    • Overview
    • Front End Development
    • Full Stack JavaScript
    • Data Analysis
    • UX Design
    • Python
  • Community Chevron
    • Code Adventures
    • Discord
    • Forum
    • Success Stories
    • Treehouse Links
    • 100 Days of Code
  • Resources Chevron
    • Explore
    • Career Toolbox
    • CodeForward
    • Free Treehouse Near Me
    • Jobs
    • Blog
    • Support
    • About
    • Learn
    • Beginner's Guide to Coding
    • Data Analysis with SQL & Python
    • Front-End Web Development
    • Python Programming Basics
    • Techdegree Bootcamp Overview
    • UX Design Fundamentals
    • AI Tools for Beginners
  • Plans For Individuals For Businesses For Schools For Libraries For Military
  • Library All Courses Tracks College Credit
  • Techdegree Overview Front End Web Development Full Stack JavaScript Python Development Data Analysis UX Design
  • Community Code Adventures Discord Forum Success Stories Treehouse Links 100 Days of Code
  • Resources Explore Career Toolbox CodeForward Free Treehouse Near Me Jobs Blog Support About Learn Beginner's Guide to Coding Data Analysis with SQL & Python Front-End Web Development Python Programming Basics Techdegree Bootcamp Overview UX Design Fundamentals AI Tools for Beginners
  • Sign In
  • Free Trial
Instagram Twitter Facebook YouTube LinkedIn

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

Mike Miello
Mike Miello
5,181 Points Posted October 19, 2013 4:19pm by Mike Miello
Mike Miello
5,181 Points
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

Bhawan Virk
Bhawan Virk
20,615 Points Bhawan Virk
Bhawan Virk
20,615 Points
October 19, 2013 11:26pm

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.

Mike Miello
Mike Miello
5,181 Points Mike Miello
Mike Miello
5,181 Points
October 20, 2013 8:49am

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!

Bhawan Virk
Bhawan Virk
20,615 Points Bhawan Virk
Bhawan Virk
20,615 Points
October 20, 2013 8:57am

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

Matt Campbell
Matt Campbell
9,767 Points Matt Campbell
Matt Campbell
9,767 Points
October 19, 2013 4:41pm

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.

David Andrews
David Andrews
17,403 Points David Andrews
David Andrews
17,403 Points
October 19, 2013 7:22pm

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.

Posting to the forum is only allowed for members with active accounts. Please sign in or sign up to post.

Tag » What Does Return Mean In Coding