Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Functions and Optionals Functions Syntax and Parameters

What do i hav eto do on, "Call the function greeting and pass it the string "Tom".?

i want it to understanding this line of code for this community to help me please because i wan to go to other parts of the course I'm taking.

parameters.swift
func greeting(name: "Tom") {
    println("Hello \(greeting)")
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Keep in mind that most challenges will require that you build upon the code that you already had. So the parameter you're accepting shouldn't have changed. It should be named person and of type String. So let's take a look:

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

greeting("Tom")

Here we have a function named greeting. It's going to accept one piece of data being sent to it by another piece of code. When that data gets there it is of type String and its name is person. It now works just like any other variable. We can now print out a line with with "Hello" and then whatever was sent into person. Now outside of the function we call it. And we do this simply by stating the name of the function and then putting what we want to pass inside the parentheses. So we call greeting and then send in "Tom". This is equivalent to saying person = "Tom". The function will then print a line with "Hello Tom". Hope this clarifies things! :sparkles: