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

Paula Carvajal
Paula Carvajal
1,679 Points

how to return a string as a greeting so when a name is entered it says "hello Tom", for example.

need some help with the last challenge of swift fucntions

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

7 Answers

Paula Carvajal
Paula Carvajal
1,679 Points

I see, it worked! Thank you very much for your time!

Okay so a few things.

1) to call a function you need to use the Syntax function() so in your example: you would need to call greeting() or more specifically greeting("Tom")`.

2) You could / should put the println inside the func so it should read: return "hello \(person)"

3) I don't think you're able to put a function inside a println-- so you may have to save greeting("Tom") to a variable like this: var functionHolder = greeting("Tom"), and then put that variable in the println OR println("Hey " + greeting("Jack"))

Paula Carvajal
Paula Carvajal
1,679 Points

Hi Avi,

thanks for your response. I tried your solution and it still gives me the same error that it was giving me before: "Your function isn't returning the correct value. I tried passing "Fred" as the parameter and got back "Fred", but expected "Hello Fred".

Which solution are you using?

Paula Carvajal
Paula Carvajal
1,679 Points

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

You are not putting anything in the greeting() function. This causes a return of void so the println is returning "hello" + void.

You need to put the println inside the function and just call the function. If you are set with using this syntax try "hello " + greeting("Tom")

Paula Carvajal
Paula Carvajal
1,679 Points

This is the challenge: Modify the definition of the function named greeting to return a String and replace the println statement with a return statement. The function should return a greeting. For example, if you pass in a string "Tom" then the return string would be: "Hello Tom".

So, should the answer be: func greeting(person: String) -> String { return person println("Hello (greeting)") }

Sorry I am new at this and have been trying this one for a long time, I appreciate your help

No problem, this is how you learn!

so what they're saying is REPLACE the println statement with a return statement (instead). So you won't have a println statement anymore. What you need is a function that receives a argument "person" (In this case "Tom") and then returns "Hello " + name.

You have two ways to accomplish this. Both are similar in that they start with defining the function greeting that accepts one argument which we're going to refer to as 'person' for the rest of the function but will be replaced with whatever value someone passes into the function. func greeting(person: String) -> String { // CODE TO RETURN HELLO NAME WILL GO HERE }

Now, since they don't want a println-- we are instead going to use return to return a string.

Option A: return "Hello " + person Option B: return "Hello \(person)"

alternatively, if this is confusing you can set a var to either "Hello " + person OR "Hello \(person)" and then just return the var example: var holder = "Hello \(person)" return holder