FAQ: Loops - The stride() Function

This community-built FAQ covers the “The stride() Function” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Swift

FAQs on the exercise The stride() Function

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I would like to say that what Codecademy calls argument labels, are actually called parameters. There are 4 types of parameters in Swift 5.3:

  • Variadic Parameters
  • Inout Parameters
  • Parameters with external names
  • Parameters with _ as their external parameter

Each of the above types are worth going through

I don’t think there is anything wrong with Codecademy’s statement (though I could be wrong). If we look at the documentation linked to by Codecademy for argument labels (https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID166), the documentation itself uses the term “argument labels”. “Parameters” are “argument labels” are not the same thing. In this particular exercise, it seems Codecademy has used the appropriate term.

Yeah, in recent Swift versions, they have changed what they used to call external parameter into argument labels.

2 Likes

Why do for-in loops print every result on a separate line? (Just a curious mind here :laughing:) Is there a way to print them on the same line?

For instance, if we have the following code,

for num in stride(from: 3, to: 0, by: -1) {
  print(num)
}

it prints as

3
2
1

But how can we make it print on the same line, such that the code outputs as below?

3 2 1

Thanks in advance!

The print function has optional parameters. If we omit these parameters, their default values are used. The default value of the terminator parameter is a newline i.e. “\n”
You can override the default by specifying the character(s) you want.
print(num, terminator: " ") will give the output 3 2 1
print(num, terminator: "") will give the output 321

You can also provide a value for another optional parameter called the separator (whose default value is a single space " ").
For example,
print("abc", "def", "ghi", separator: "...", terminator: "!!!") will print:
abc...def...ghi!!!

@mtrtmk Thank you!! I understand it much better now. I will also keep the separator in mind for the future and play around with both a bit. Again, thanks! :smile:

So what should I call them, argument labels? Do code languages normally change there thermology? Thanks for the bringing this to my attention !!

Hey all! I have a question about the contents in the “Loops: The stride() Function” lesson…

In the example:

The argument label “to: 6” is specified in the stride function. In the output, 6 is not included. In the lesson description it says, " * The second argument, to: 6 is the end of the sequence (6). But notice, we didn’t print out 6 which means this end number is not included."

I want to clarify this a bit further. Does this output mean that:

  1. Generally the item in the to: argument (the sequence end) is never included in the stride() function output?
  2. Or, is it that in this example 6 is not actually in the array that is being looped?

Thank you! :yellow_heart:

Documentation for stride(from:to:by:)

Returns a sequence from a starting value to, but not including, an end value, stepping by the specified amount.

Parameters
start
The starting value to use for the sequence. If the sequence contains any values, the first one is start.

end
An end value to limit the sequence. end is never an element of the resulting sequence.

Contrast this with stride(from:through:by:)

Returns a sequence from a starting value toward, and possibly including, an end value, stepping by the specified amount.

Thank you so much! That is super helpful!

And thank you for including the other stride() option!

1 Like