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

Challenge Task 2 of 3

This is part of the iOS program and is asking me to create a function that greets people with hello. Gives an example for "Hello Tom" Been stuck on this forever! If anyone could help walk me through what to do that would be great. What I have....

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

2 Answers

Ben Griffith
Ben Griffith
5,808 Points

Hey Cory,

Let's start with what you created in the 1 step.

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

Step 2 of 3 asks the following:

Modify the println statement within the greeting function to use the variable person. For example, if you pass a person named "Tom" to the function then it should display: Hello Tom. (Hint: you must use string interpolation).

So your first step is to use string interpolation. Here's a short extract from Apple's Swift Guide

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Each item that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash.

So in our example, since we're passing in person, which will contain the name we want to display, you need to do the following:

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

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

1.) you forgot to close out your "" in 2nd line.. should be: var person = ( "Hello + person")

2.)you forgot the backslash in the 3rd line so it should be:

println("Hello (person)") }