Programming in Swift: Functions and Types · Overloading | raywenderlich.com


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:

  1. getPassStatus(_ grade:) is called as getPassStatus(4). This is very confusing because you can’t tell what 4 actually is in this case based on the function call after all.
  2. getPassStatus(grade:) is called as getPassStatus(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.
  3. getPassStatus(for:) is called as getPassStatus(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!

1 Like

Hi, would like to understand why do we need to use overload when it seems to cause confusion easier

1 Like

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.