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
Dennis Lander
1,802 PointsSwift functions extra credit question - could you check my code?
The question is:
Write a function called fullName which takes two String parameters: firstName and lastName. It concatenates the two parameters and returns them as the fullName.
And this is what I came up with:
func fullName(firstName: String, lastName: String) -> String {
return("My name is (firstName) (lastName)")
}
println(fullName("Joe", "Smith"))
This isn't the exact way the code looks in Xcode - the return line is indented and there aren't any spaces between the lines.
It works, i.e. it prints "My name is Joe Smith" but it doesn't seem like the best code to me. What do you think? Are there better alternatives?
Thanks in advance.
4 Answers
Jason Hernandez Berland
1,575 PointsHey Dennis Lander , you can also implement it like this and do away with the println statement
func fullName(firstName: String, lastName: String) ->String{
return("\(firstName) \(lastName)")
}
fullName("James", "Dean")
Pavel Fomchenkov
20,897 PointsYour code seems OK, but the function would be more universal if you move the "My name is " part to println():
// Hint: You can use Markdown Cheatsheet link right under the input box!
func fullName(firstName: String, lastName: String) -> String {
return "\(firstName) \(lastName)"
}
var fName = fullName("Joe", "Smith")
println("My name is \(fName)")
Keep going!
Dennis Lander
1,802 PointsThanks! One more question: how do you put your code in this black box like in your replies? I played around with it in my post and I got only one line in a box like that. Much appreciated!
Jason Hernandez Berland
1,575 PointsCheck out the markdown cheatsheet