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

iOS Swift Functions and Optionals Functions Function Return Types

I don't understand what return does

What does it mean by return something in the function? What does that return do and what's the point of it? All of this seems confusing to me still.

Another part of this question is, isn't return something the same thing as printing it out with println? What's the difference if you can just use the print command rather than return?

1 Answer

Hi Roland!

When something is returned from a function it can then be assigned to a variable or used in a conditional statement (such as an if statement). The code below assigns the return value of the method 'getUsername()' to the variable 'username'

String username = getUsername();

A return value is a way of getting a reply back from a method instead of it just running and returning nothing.

A good way to think of returns is like a phone call to a pizza shop! So if you phone the Pizza Shop think of this as running a method called 'phonePizzaShop()' and what this method, or phone call, will return is a pizza!

I hope this helped you out and don't hesitate to ask any more questions!

-Luke

Thanks! But what's the difference between getting a return and just printing out a line that says the same thing as the return?

No problem!

Just printing out the value to the terminal without a return value doesn't let you set the value to a variable or use it use in conditional statements.