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

James Robertson
James Robertson
1,934 Points

Return Types code challenge.

I have been stuck on this challenge for a while and cant work out why the output is not Hello Tom, even though I have specified Println("hello (greeting)").

The aim of the challenge was to create a return type to display Hello Tom. I have run out of ideas :(

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

println("Hello \(greeting)")
James Robertson
James Robertson
1,934 Points

Don't worry, solved it! Didn't need the Println statement after all... this code worked:

func greeting(person: String) -> String {

    let greeting = "Hello" + person"
    return greeting

}

3 Answers

rtprjct
rtprjct
30,548 Points

Your function is correct, when you call it, you need to give it the string "Tom"

println("Hello " + greeting("Tom"));
Mike Atkinson
Mike Atkinson
6,882 Points

Yes as artie91 said, you need to pass an argument to the function.

When declaring, the part in the round brackets is the parameter. To call a func, you need to supply it with the required argument(s) to match the parameter(s).

For example here "SomeString" is the argument to match the parameter (person: String).

//declaring with one parameter
func greeting(person: String) {}
//calling with one argument
greeting("SomeString")

a func can have 0,1, or more parameters.

eg.

//Declaring
func newFunc() {
    println("Funky")
}

//calling
newFunc()
//prints "Funky"

Highly recommend about functions here, at the apple developer library.

Whitman Huntley
Whitman Huntley
6,811 Points

This did it for me.

func greeting(person: String) -> String {

let greeting = "Hello \(person)"
return greeting

}

println(greeting("Tom"))