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 Syntax and Parameters

Oliver Burnand
Oliver Burnand
935 Points

func greeting(person: String) { println("Hello \(greeting(person: String))") } can't see what i'm doing wrong?

any help

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

5 Answers

Hi Oliver,

Let's take this step-by-step. You start with:

func greeting() {
    println("Hello")
}

You need to add a prameter to the function which is called person and is a String. That looks like:

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

Which is what you have done. Next up, the question says "Modify the println statement within the greeting function to use the variable person". So that needs you to incorporate the paramater, above called person, into the output of the println statement. That needs you to use string interpolation as you have nearly done:

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

Lastly, the challenge is to call the function using the paramater "Tom" - that's pretty straightforward:

greeting("Tom")

I hope that all makes sense!

Steve.

No problem! :-)

Oliver Burnand
Oliver Burnand
935 Points

The next task asks to create a function that RETURNS the greeting rather than just printing it - when i replace 'println' with 'return' and error comes up. Any help?

Yep sure ...

First, you'll have to set the function up to return a string ...

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

So that skeleton now takes a String as a parameter and returns one too. But we need to amend the body of the function too:

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

Without seeing the challenge, that's the best I've got - let me know if it works!

Thanks,

Steve.

Oliver Burnand
Oliver Burnand
935 Points

That worked :) , thanks again!

Glad it worked! :-)