zeros(Float64, 2) or zeros(2, Float64)

julia> a  = zeros(Float64,  2)
2-element Vector{Float64}:
 0.0
 0.0

julia> a  = zeros(2, Float64 )
ERROR: MethodError: no method matching zeros(::Int64, ::Type{Float64})

Closest candidates are:
  zeros(::Union{Integer, AbstractUnitRange}...)
   @ Base array.jl:631

Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

So the order of the parameters is essential? Is there any rule in construction such typical objects?

Yeah, the type always comes first.

1 Like
help?> zeros
search: zeros count_zeros set_zero_subnormals get_zero_subnormals leading_zeros trailing_zeros zero iszero RoundToZero

  zeros([T=Float64,] dims::Tuple)
  zeros([T=Float64,] dims...)

  Create an Array, with element type T, of all zeros with size specified by dims. See also fill, ones, zero.

Yes, zero is a function that constructs an array containing zeros, and the docstring indicates that it takes the element type as an optional first argument.

1 Like
zeros([T=Float64,]   dims::Tuple)

means I can invoke the function as

a = zeros(Float64, (2,3))

while

zeros([T=Float64,]   dims...)

means I can invoke the function as

a = zeros(Float64, 2,3)

Is my understanding right?

That is correct. Inside of a function definition, trailing dots ... indicates a variable number of arguments (docs for Varargs).

In this case zeros(Float64, 2, 3) yields a two-dimensional array of size 2x3, zeros(Float64, 2, 3, 4) yields a three-dimensional array of size 2x3x4, etc.

1 Like

Here’s a description of the preferred order of function arguments
https://docs.julialang.org/en/v1/manual/style-guide/#Write-functions-with-argument-ordering-similar-to-Julia-Base

6 Likes