Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Functions Function Return Types

Michael Bates
Michael Bates
13,344 Points

How do I return a String?

I am currently stuck on creating a Return function. I am getting an error message about "Tom" not matching the String. How do I correct that issue? Thanks!

returns.swift
func greeting(person: String)-> String{
return person
}
let person = (Tom)
println("Hello \(person)")

4 Answers

Brenden Konnagan
Brenden Konnagan
16,858 Points

ok... so first in this exercise you are assigning the result of a function to a value.

func greeting(person: String) -> String {
return person
}

let person = greeting("Tom")
print("Hello \(person)")

So... you can read it like this: The function greeting requires a String value labeled person, and it gives you a string value. You are assigning this string to a constant named person. At the time of declaration, you are providing the greeting func with a value that is a String literal (in quotes). Finally when printing, the code in the function is executed and printed to console using string interpolation.

Michael Bates
Michael Bates
13,344 Points

I finally found out what the error was!! I took the line of code from the print function and put it in the return function like so:

func greeting(person: String) -> String{ return ("Hello (person)") .......

Thank you very much for your help! I really appreciate it! :)

(For some reason the backslash isn't appearing in the comment, but it is definitely there when I typed this out)

Brenden Konnagan
Brenden Konnagan
16,858 Points

Hello Michael!

The first issue that I am looking at with your code is that your declaration of the constant person is incorrect. Swift compiler will not know what you mean. You can initialize a string literal by the following syntax:

 let person = "" 

Also, the print function needs to call the greeting function to get the result. It currently is not calling the greeting function.

Brenden Konnagan
Brenden Konnagan
16,858 Points

You can also declare the person using the greeting function. i.e.

let person = greeting("Tom")
print("Hello \(person)")

I believe this is more what you are looking for.

Make sense?

Michael Bates
Michael Bates
13,344 Points

Ahhhhh.... I was very close before this instance and had everything except the greeting function declared. It turns out that I backtracked too far missed even more trying fix my error in the above lines of code!

Thank you very much for the help!

Michael Bates
Michael Bates
13,344 Points

I am still getting an error it seems....

Brenden Konnagan
Brenden Konnagan
16,858 Points

Ok. I will be happy to help more. Post what you got and I will walk through it with you.

Michael Bates
Michael Bates
13,344 Points

Unfortunately I can't do it from my Mac as I am out of town, so I do not have the recent attempt readily available. but it still seems like something is out of place as now the most recent error was that it just calls Tom, but not Hello Tom.

So maybe just go from the code above?