Road to Julia 2: Variables, Types and Functions

Variables
In Julia, variables are names of values stored in the computer’s memory. They are instantiated by writing the name of the variable followed by an equality sign and finally the value which is being stored, e.g.
julia> x = 1
1
julia> x + 1
2Variables in Julia are optionally-typed – that is, data types do not need to be defined, however doing so may give a boost to performance. The advantage of this methodology over being either purely dynamically-typed (as in Python) or statically-typed (as in C/C++) is that Julia can be used for fast prototyping while also leaving the option for optimisation easily attainable.
Types
Types are assigned to variables using the :: operator and by convention all types have names that are proper case (capitalised first letters of words), e.g. Int, String, Array, BigFloat. This can then be implemented as x::Int = 1 in a script, however Julia does not currently support global variables having declared types such as that found in a REPL;
julia> x::Int = 1
ERROR: syntax: type declarations on global variables are not yet supportedFunctions
Functions allow procedures to be accessed from elsewhere in a program as well as being called multiple times without needing to be rewritten. In Julia functions are delimited syntactically by
function myfunction(arguments)
...
endFor a value to be received from the function, it must end with the return keyword (anything following the return keyword will not be executed), e.g.
function add(x, y)
return x + y
endThere is also an equivalent, more terse notation for defining short functions;
add(x, y) = x + yJulia allows multiple definitions of the same function name with different argument data types, of which it then calls the appropriate for the arguments passed e.g.
julia> f(x::Int, y::Int) = x + y
f (generic function with 1 method)
julia> f(x::Float64, y::Float64) = x * y
f (generic function with 2 methods)
julia> f(1, 2)
3
julia> f(1.0, 2.0)
2.0The data type of the value returned from a function may also be specified by appending the return type to the function’s parentheses:
julia> f(x, y)::Float64 = x + y
f (generic function with 1 method)
julia> x = f(1, 2)
3.0
julia> typeof(x)
Float64The arguments for a function fall into two categories: positional arguments and keyword arguments. As the name suggests, values of positional arguments are assigned according to the order in which they are passed, e.g.
julia> f(x, y) = println(y, "\n", x)
f (generic function with 1 method)
julia> f(1, 2)
2
1Conversely, keyword arguments are passed as named variables for which order does not matter. In Julia, all positional arguments are listed first, then delimited by a semicolon before the keywords are then passed. In the following example, a and b are positional arguments while c and d are positional arguments:
julia> f(a, b; c, d) = a + b + c + d
f (generic function with 1 method)
julia> f(1, 2; c = 3, d = 4)
10Quite often one might wish to have default values upon which to fall back, allowing some arguments to not need to be passed. In such a scenario, one assigns a value for the argument within the function parentheses, e.g.
julia> f(a = 1; b = 2) = a + b
f (generic function with 2 methods)
julia> f(10)
12
julia> f(; b = 10)
11
