Programming in Swift: Functions and Types · Challenge: Overloads & Parameters | 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/6

i keep on downloading the marials and it only gives me the intro ones

None of the materials have exercises for this course. They all say “exercises and challenges for Part 1 of the Programming in Swift: Functions and Types course,”

@brocket12 @codes Playground files can contain more than one page! There is one playground file for each part of this course, and one page in those playgrounds for each episode in the part.

To see all of the pages, open the Navigator just like you would to see all of the files in a regular Xcode project. You can open and close the Navigator with the keyboard shortcut Command 0, or use the button circled in the screenshot below.

Navigator

Hey Catie,

Thanks for the quick reply! However, some of the exercises are already completed. For example, the Challenge for Overloads & Parameters was already filled out upon clicking on it

Hi! I just took a look and found a couple lines to remove (I’ll get the downloads updated shortly), but the meat of the challenges are not completed in the version you already have.

Make sure you’re working in the “Begin” playground. Also, a lot of the challenges in this course will have some code already in the playground for you to modify or use in some way. That’s the case for the Overloads & Parameters challenge!

Hi Catie,

You’re right! The majority of the exercises are there - sorry for the confusion. Please let me know once you’re able to push the updates. Much appreciated!

Ok, updates are up! :]

In Challenge 3: I didn’t understand the “withPoints points” thing.

Why did I have to use “withPoints” when calling the func? Which lesson covers this?

And by “right parameter” I think you mean “correct parameter”?

In Challenge 1: I don’t understand the “by” in the parameters. I tried to search for this, but the word is too generic and I don’t know what the concept is here.

Hi! In both of your questions, you’re asking about argument labels. Not all programming languages use argument labels, but Swift does!

You can learn about them in this episode of the Swift Fundamentals course: https://www.raywenderlich.com/5539282-programming-in-swift-fundamentals/lessons/35

I also reviewed them in the second episode of this course:
https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/2

To sum it up here: Parameter names are how you refer to the parameter values inside the function body. When you call a function that has parameters, you pass in arguments. The parameter names turn into argument labels, unless you put an alternative in the parameter list right before the parameter. You can either say you don’t want an argument label with an _ or specify a different argument label.

Here’s a generic kind of code diagram of argument labels and parameter names:

func functionName(argumentLabel parameterName: ParameterType) { 
  // parameterName is used in here 
}

// argument label is used when you call the function
functionName(argumentLabel: argument) 

For your specific questions, both withPoints and by are argument labels! withPoints acts as the argument label for points, and by is the argument label for multiplier.

I’m not sure what you mean about “right parameter”. I couldn’t find where I said this in the video!

Thanks very much. I missed that.

By “right parameter”, I meant in the download materials. Part 6, Challenge 3, step 1.

1 Like

Oh! Apologies.
Yes, I did mean “correct parameter,” not “the parameter on the right side of the parameter list”.

How the compiler differ two overloaded functions? I’ve created two kinds of functions, one with varidadic parameters and one with no labels, and, when i call them, they have no difference in syntax. The only difference was that i’ve chosen different types of functions in Xcode popup. Does this means, that there are some hidden additional information in Xcode
Снимок экрана 2021-06-14 в 15.46.05
?

@arpogromsky The compiler knows exactly which function version to use over here because they actually have different names in this case: multiply(_:_:) and multuply(_:).

Things get trickier with variadic parameters if the function has the same name in both cases like this:

func multiply(_ numbers: Int...) -> Int {
  print("This is a function with variadic parameters")
  var mult = 1
  for num in numbers {
    mult *= num
  }    
  return mult
}

func multiply(_ number: Int, _ multiplier: Int) -> Int {
  print("This is a function with no labels")
  return number * multiplier 
}

multiply(2,3) // "This is a function with no labels"
multiply(2,3,4) // "This is a function with variadic parameters"

The compiler chooses the multiply(_:_:) overload if the function call has exactly two Int arguments since that version of the function prototype always requires exactly two Int parameters. It uses the multiply(_:) overload instead if the function call has any other number of Int arguments because the function signature requires an Int... variadic parameter in this case.

Since both function overloads do exactly the same thing in this case, it makes perfect sense to go ahead and just use the varadic parameter overload for all function calls as follows:

func multiply(_ numbers: Int...) -> Int {
  print("This is a function with variadic parameters")
  var mult = 1
  for num in numbers {
    mult *= num
  }    
  return mult
}

multiply(2,3) // "This is a function with variadic parameters"
multiply(2,3,4) // "This is a function with variadic parameters"

The multiply(_:) overload returns 1 even if the function call has no arguments since numbers is an empty array in this case. You can easily fix that by changing the function return type from Int to Int? like this:

func multiply(_ numbers: Int...) -> Int? {
  print("This is a function with variadic parameters")
  guard !numbers.isEmpty else {return nil}
  var mult = 1
  for num in numbers {
    mult *= num
  }    
  return mult
}

multiply() // nil

You use guard to return nil if numbers is empty. This only happens for the multiply() function call.

Please let me know if you have any more questions or other issues about the whole thing when you get a chance. Thank you!