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

Mikkel Storch
Mikkel Storch
1,955 Points

What is the exact meaning of "return" in functions?

I've watched the video "Function Return Types" a couple of times but just can't see to figure out the exact meaning of "return". Can someone explain it to me in the most educational way possible step-by-step?

2 Answers

Dennis Parussini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Parussini
Treehouse Project Reviewer

It's actually pretty simple. A method or function returns a value to you so you can use the value returned to you outside of the method. In Swift it would look something like that:

func printName(name: String) -> String {
return name
}

var name = printName("Tom")

If you wouldn't return the name you couldn't use it to assign the name to the variable name. Just copy the code into a playground to see what I mean. :-)

I hope I could help you with that, for further questions, please ask. ;-)

Mikkel Storch
Mikkel Storch
1,955 Points

Okay, but when you write "(name: String) -> String)" doesn't that mean that you create a string called "name"? But then you create a variable called name outside of the function? Just don't quite get it...

Dennis Parussini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Parussini
Treehouse Project Reviewer

No, (name: String) -> String is part of the function.

(name: String) is the parameter the function takes. If you call the function you can then type a name where now (name: String) is. -> String means that the function has to return a String, in this case "name" is a String so you can return it to use it outside of the function. I named the variable "name", but it could have been any other name, like title or something. You can call the function without assigning it to a value of course. But this way, if you type "name" after assigning it the function you see the name you provided to the function in the sidebar of your Xcode Playground.

what the name: String in the parentheses says is that you take a string input from the user, and you use it with the name "name" (or any other name you would enter before ':'). You don't create a variable, you just ask an input of type string from the user and call it "name". After you give it a name, you can use the input as a variable with the name you gave it within the function scope. If you need any more explanation just say and i will try to explain it in a simply way.

Itai

The meaning of the return keyword is to give the user output after we did all we wanted to do in our function. you can use the return keyword in two ways:

  1. To stop the function.
  2. to return something (as the name says). If you don't understand my explanation tell me and i will try explain it to you in a simpler way.

Itai

You're welcome. if you need anything else, ask me and i will answer you as fast as i can.