This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/4
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/4
Hi there! I have a question about the next:
func getPassStatus(for grade: Int) → Bool {
grade >= passingGrade
}
what the ‘for’ keyword mean?? because the name of the parameter is grade, the type is Int
thanks!
@yevh You actually choose the best function prototype based on its corresponding call after all:
-
getPassStatus(_ grade:)
is called asgetPassStatus(4)
. This is very confusing because you can’t tell what 4 actually is in this case based on the function call after all. -
getPassStatus(grade:)
is called asgetPassStatus(grade: 4)
. This is definitely better than the previous signature because you can tell for sure and for good indeed that 4 is actually a grade this time based on its associated label after all but it is still unclear how the function really uses the argument itself based on its call. -
getPassStatus(for:)
is called asgetPassStatus(for: 4)
. This is actually the best version of all after all since it is definitely clear now what the function really does based on both its call and associated label this time: it simply returns the pass status for a certain grade.
Please do let me know if you actually have any questions or issues about the whole thing when you get a chance after all. Thank you!
I promise to never over overload!
Hi, would like to understand why do we need to use overload when it seems to cause confusion easier
Hi! Thanks for your question.
I would say that you’ll rarely need to write function overloads. It’s more important that you know you can write them, and that you will find them in Swift and many of Apple’s frameworks.