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

Abdurahman Sharif
Abdurahman Sharif
1,205 Points

trouble with functions and return functions. when do i use what?

extra credit, its asking us to : Write a function called fullName which takes two String parameters: firstName and lastName. It concatenates the two parameters and returns them as the fullName.

this is what i have on my playground. but the end product is not what i want. any suggestions and explanation on why its not working for me?

this is what i have:

func fullName (firstName: String, lastName: String) { let name = firstName + lastName println("My Name is (name)") }

fullName(John,Doe)

but it wont print on my console output

please help

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

You missed __ in your println, also, it's preferable to have a space between firstName and lastName.

func fullName(firstName:String, lastName:String) {
  let name = "\(firstName) \(lastName)"
  println("My name is \(name)")
}

fullName("John", "Doe")   // => "My name is John Doe"
Abdurahman Sharif
Abdurahman Sharif
1,205 Points

oh, i forgot the "" when i entered my name.. but will, why is the way i did it or you did it a little different then when the instructor showed on the video, is it ecause he was using integers?

the example in the video was

func calculateArea (height: Int, width: Int) { let area = height * width println(" the area of the room is (area)") }

calculateArea(10,12)

i mean he didnt use "(height) * (width)"

he just wrote hight * width.. why is ours so different when we used a String

William Li
William Li
Courses Plus Student 26,868 Points

Abdurahman, String is String, Integer is Integer, each data type has its own unique sets of operations and how you need to use them. You can't guess how one data type should be used based on the knowledge you have on another data type. In the example let area = height * width, it's assigning the product of height and width and assign to area, they can be used this way because it's Integer, and it supports multiplication. let name = "\(firstName) \(lastName)", here what we do isn't so different, except we're dealing with String here, so the operations is different than dealing with Int, that's how we combine two strings together and add a space in the middle. :)

Abdurahman Sharif
Abdurahman Sharif
1,205 Points

oh ok.understood. thank you again

Maybe you should try

println("My name is \(name)")
Abdurahman Sharif
Abdurahman Sharif
1,205 Points

oh im sorry, yes thats what i had

Abdurahman Sharif
Abdurahman Sharif
1,205 Points

this is what i meant to post

func fullName (firstName: String, lastName: String) { let name = firstName + lastName println("My Name is (name)") }

fullName(John,Doe)