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

Justin Lim
Justin Lim
1,391 Points

Can I create a function called "intro" for example and include my name and age? Can it accept both -> String and -> Int?

func intro(firstName: String, lastName: String, age: Int) -> String -> Int { return ("My name is (firstName) (lastName), and I'm (age) years old") }

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

1 Answer

You definitely can Justin! It can look something like this:

func intro(firstName:String, lastName:String, age:Int) -> String {
    return "My name is \(firstName) \(lastName), and I'm \(age) years old"
}

A point to note in regards to your code is that anything within the parenthesis following the method name is the parameters the method will accept, and anything after the -> is the return type. I noticed that you wrote "-> String -> Int".

If you wanted to return multiple types in Swift, its pretty easy to do so. The way to do so is to return a tuple which could encapsulate both a String and an Int within it. Treehouse has a really good video on this topic, hope this helps!

Justin Lim
Justin Lim
1,391 Points

Sweet thanks for the help!