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

Mehran Amini Tehrani
Mehran Amini Tehrani
1,523 Points

iOS functions

Is the answer for question below right?

// Write a function called fullName which takes two String parameters: firstName and lastName. It concatenates the two parameters and returns them as the fullName.

func fullName (firstName: String, lastName: String) ->String { return("firstName", "lastName")

}

fullName("Mehran", "Amini")

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points
func fullName (firstName: String, lastName: String) -> String {
   return("firstName", "lastName")
}

fullName("Mehran", "Amini")

Although your function is expected to return only a single string -> String, you are returning two strings. Thus, the return type you have declared and what you are actually returning does not match. Moreover, you are not returning the variables, but plain strings "firstName" and "lastName". This is how it would work:

func fullName (firstName: String, lastName: String) -> String {

   // Version 1: string interpolation
   let name = "\(firstName) \(lastName)" 

   // --- alternatively ---

   // Version 2: + operator
   let name = firstName + " " + lastName

   // Now return the constant name, containing a string
   return name
}

// Now we are calling the function
let name = fullName("Mehran", "Amini") // name is now "Mehran Amini"

Hope that helps :)