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

Pierre Robert
Pierre Robert
5,717 Points

Cannot compile a function

The task is 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).

I coded the following, but it doesn't compile

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

Thanks for helping, Pierre

parameters.swift
func greeting(person: String) {
    var person = Tom

    println("Hello \(person)")
}

3 Answers

Mark Cranston
Mark Cranston
1,817 Points

You are just doing this a little out of order... You are declaring a variable inside the function, instead you need to call the function which is done using "functionName(input)"

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

//Call the function
greeting("Tom")
Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Mark Cranston has the right answer. The reason why the OP code did not compile, though, was that

var person = Tom

was meant to be

var person = "Tom"

This is just to clarify your compile time error, other than that Marks answer is definitely the way to go!

<small>P.S. Watch out, with the release of Swift 2 println has become print </small>

Pierre Robert
Pierre Robert
5,717 Points

Thanks to both of you. Goes to show that programming teaches one to be rigorous.

Pierre