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

Jivesh Sehgal
Jivesh Sehgal
742 Points

how do you use this in swift2 ?

Ive updated my Xcode and now Im more confused than I was before I started this course? Please help

4 Answers

Richard Lu
Richard Lu
20,185 Points

Hi Jivesh,

From what I understand, the question you're asking is how to print a functions return type in Swift 2.0. The println function in the latest Swift has been shortened down to print. In this videos example, he has

println("Area = "\(calculateArea(10, 2))")

So in Swift 2.0, it should look like this.

print("Area = "\(calculateArea(10,  2))")

There has also been changes to calling a function method (mostly to do with external names), for example:

In Swift 1.2, you could of called a method like this:

func calculateArea(height: Int, width: Int) -> Int {
    return height * width
}

calculateArea(100, 2)

Now in Swift 2.0, you could must provide the external parameter names (excluding the first one by default)

calculateArea(100, width: 2)

If the preference was to include/exclude the name, use underscore (_) next to the parameter name. Otherwise you would need to name your external parameters:

Exclude

func calculateArea(height: Int, _ width: Int) -> Int {
    return height * width
}

calculateArea(100, 2)

Include

func calculateArea(heightvalue height: Int, widthval width: Int) -> Int {   // NOTE: heightval and widthval may also be the same names as the parameters
    return height * width
}

calculateArea(heightvalue: 100, widthval: 2)

Everything else in this video should work the same. Hope I've helped. Happy Coding! :)

San Francisco
San Francisco
28,373 Points

This seems to still return an error

import UIKit

print("Swift Functions")

func calculateArea(height: Int, width: Int) -> Int {

    return height * width
}


print("Area = \(calculateArea(100,120))")

calculateArea(100,120)
Richard Lu
Richard Lu
20,185 Points

Hi San Francisco,

I have edited my solution above. In your case I would have the function to look like this:

func calculateArea(height: Int, _ width: Int) -> Int {   // the underscore tells the compiler I don't care about this external parameter name
    return height * width
}

print("Area = \(calculateArea(100,120))")   // notice, there is no need for the external parameter names

Happy coding! :)

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

If I understand well, in Swift 2 println == print. So now, how do you print to the next line in swift 2 ?

Richard Lu
Richard Lu
20,185 Points

Hey Emmanuel,

Since println == print, it actually automatically prints the new line. If you really need to add another new line in the same string, you can use the special character '\n' and here's how to go about it.

print("a line\n")

Some more information on that would be located here: http://tinyurl.com/orlz6y8

What you're looking for specifically is under "String Literals".

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

Thanks Richard for your help!