We have met functions in previous chapters and know how a function() … end
block works and that there is a convenient one-line syntax for the simplest of cases:
That is, sq(x) = x*x
is exactly equivalent to the following:
function sq(x) y = x*x return y end
The variable y
is not needed (of course). It is local to the sq()
function and has no existence outside the function call. So, the last statement could be written as return x*x
or even just as x*x
, since functions in Julia return their last value.
Functions are first-class objects in Julia. This allows them to be assigned to other identifiers, passed as arguments to other functions, returned as the value from other functions, stored as collections, and applied (mapped
) to a set of values at runtime.
The argument list consists of a set of dummy variables, and the data structure using the ()
notation is called a tuple. By default, the arguments are of type {Any}
, but explicit argument types can be specified,...