+ 3
Is this functions workout correct?
Functions w/ Return Values Exercise: Verbing Your Noun Write a function that returns a sentence like âRow, row, row your boatâ when given a verb and a noun argument. The function should look like this when you call it: let line = openingLine(verb: "Row", noun: "Boat") func makeASentenceWithA(verb: String, noun: String) -> String { Â Â Â Â return "\(verb), \(verb), \(verb) your \(noun)." } makeASentenceWithA(verb: "row", noun: "boat") This particular line is what throws me off: let line = openingLine(verb: "Row", noun: "Boat") Is this correct? I'm a little confused about this challenge. What is it telling me?
7 Answers
+ 1
No problem. Just happy to help you out.
I recognize the lesson youâve submitted. Itâs from Intro to App Development with Swift. Itâs a great course, and I recomend that you push through. Some things where they only scratch the surface, will be explained later. It wonât hold your hand, as youâll find out at later excercises. I advice you to learn from several sources at once. Thereâs a youtube series by iBrad that can help you if you get completely stuck, though.
+ 1
This doesnât look correct. To fix this, you can either rename your function to «func openingLine», OR you should change this line: let openingLine = makeASentenceWithA(verb: "Row", noun: "Boat")
Notice that I changed «let line» into «let openingLine» instead.
+ 1
It seems to me like youâre more confused to why we have functions in the first place. Iâd recomend reading more about functions from several sources for clarity.
The gist of it, however, is pretty simple. Functions should be considered as a piece of code that is reusable. The main benefit should be that it saves us both time and lines of code, while also being easy to read. This makes your intention behind the specific code clearer as well.
«let line» is simply a constant which we choose to contain the function in this case. Although, itâs visible in the playground, you wonât be able to call the function correctly, unless itâs contained in a constant or a variable. This will throw an error that the function was unused.
+ 1
I appreciate your long response. thanks for taking time from your day. I totally get why functions are used. I have been taking multiple courses using multiple apps, sites, and books. Iâm doing cascading lessons to help with memory and understanding the content in different delivery. I just didnât get why they put let line before the function. This particular lesson (not Sololearn) is the only lesson ive seen yet that keeps putting stuff like let line = before the function and its throwing me off. I thought to call a function you simply write the function name followed by ().
+ 1
spot on, thanks alot!
0
Im struggling with:
let line = openingLine(verb: "Row", noun: "Boat")
I think the "let line" is the part the throws me off. why is it there? Is it just saying the line: openingLine(verb: "Row", noun: "Boat") is a constant line that reads as follows i.e. openingLine(verb: "Row", noun: "Boat") ?
I may be overthinking it but I don't get why they bother saying, "...the function should look like the let line" if "let line" part isn't in the function. It throws me off.
let line = openingLine(verb: "Row", noun: "Boat")
0
Is the correct?
func openingLine(verb: String, noun: String) -> String {
print("Row, \(verb), \(verb) your \(noun).")
return "Row, \(verb), \(verb) your \(noun)."
}
openingLine(verb: "row", noun: "boat")
***please ignore print and return unless you have some advice about using both at the same time. im using both because I'm going back and forth between Xcode and Sololearn playgrounds.
I'll throw in my other variation of the above function.
func makeASentenceWithA(verb: String, noun: String) -> String {
print("Row, \(verb), \(verb) your \(noun).")
return "Row, \(verb), \(verb) your \(noun)."
}
makeASentenceWithA(verb: "row", noun: "boat")