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 Creating a Function

Fedor Andreev
Fedor Andreev
11,203 Points

I really don't understand why I need to use this \(area)?

Is it really impossible to print a string "Hello" and just add an integer + b when b =2?

From a logically perspective, it makes sense to me. Computer, print Hello on the screen. And don't forget to add the variable b on the right side which contains the number 2!

1 Answer

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Hi, while using print() (or println() in swift 1.2) you pass arguments (what to print) to the function and they have to be of the same type, meaning that you can't do:

var b = 2
print("Hello " + b)     //this will raise an error because "Hello" is a String and b is an Int

BUT you can always cast between types of data, and something like this will work:

var b = 2
print("Hello " + String(b))     //this will print 'Hello 2' to the console

Although you could cast types like that I would still recommend normal string interpolation ("\(area)") just because it is pretty conventional among programmers and easy to read along with better functionality as you can use functions or class methods instead variables and pass them to the print() function:

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

print("The area is: \(calculateArea(2, height: 6))")      // will print: 'The area is: 12'