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

William Hartvedt Skogstad
William Hartvedt Skogstad
5,857 Points

I can't really understand what I should be doing here...

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".

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

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hello, William Hartvedt Skogstad

Modify the definition of the function named greeting to return a String and replace the println statement with a return statement.

This challenge is asking for couple of things

  1. replace the println statement with return statement.
  2. since the println has been replaced with return, you should also set the proper return type for this function.
func greeting(person: String) -> String {  // function now has return type String
    return "Hello \(person)"   // replaced println with return statement
}

Hope that helps.

Chris Shaw
Chris Shaw
26,676 Points

FYI: The parentheses aren't required for the return statement, you can simply use the following.

return "Hello \(person)"
William Li
William Li
Courses Plus Student 26,868 Points

oh right, changed the println to return but forgot to remove the parentheses. updated.

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello William,

To tell the function the type you want to return, you'll add the following to the method signature.

func greeting(person: String) -> String

Instead of calling println, you'll want to use the return keyword like so.

return "Hello \(person)"

I hope this helps.