Using step in the range function

I’m trying to understand how the range function works. Because I know that you can define each value. (the examples are from documentation)

range(stop=10, step=1, length=5)

And I know that you can forgo some of the keywords.

range(1, 100, step=5)

^^^This works.

range(1, 5, 5)

This works too, where the input is assumed to be start, stop, length.
But I’ve been unable to use step in this function without defining it. Is it possible to do so?
Also is it better to define the input values or not? Is it up to preference?

Looking at the signatures in the documentation

help?> range
search: range logrange angle rand randn rangebars LinRange rangebars! cancel ztickrange ytickrange xtickrange

  range(start, stop, length)
  range(start, stop; length, step)
  range(start; length, stop, step)
  range(;start, length, stop, step)
(...)

there is no version where step is a positional argument, so to directly answer the question: no, this is not possible.

You can use different range-types, though, like a StepRange (a.k.a. start:step:stop):

julia> StepRange(1, 1, 5)  # start, step, stop
1:1:5

julia> 1:1:5 |> collect
5-element Vector{Int64}:
 1
 2
 3
 4
 5

(Assuming by input value you mean the keyword argument (keys)), yeah, I’d say this is a personal preference. If you use the function a lot, you’ll likely get tired of typing out start = every time :slight_smile:

3 Likes