3.4 Variables

A variable is a name that will be associated to a value through a memory space; these are case-sensitive.

3.4.1 Defining variables

Variables are defined and modified using =, see some examples below.

x = 10
print(x, ", ", typeof(x))
# 10, Int64

x = 10.0
print(x, ", ",  typeof(x))
# 10.0, Float64

x = "Hello World!"
print(x, ", ",  typeof(x))
# Hello World!, String

x = 1 + 2im
print(x, ", ",  typeof(x))
# 1 + 2im, Complex{Int64}

x = 1 // 2
print(x, ", ",  typeof(x))
# 1//2, Rational{Int64}

3.4.2 Define more than one variable

More that one variable can be defined using tuples syntax.

(a, b) = (1, 10)
print(a, ", ", b)
# 1, 10

a, b = 1, 10
print(a, ", ", b)
# 1, 10

(a, b) = 1, 10
print(a, ", ", b)
# 1, 10

a, b = (1, 10)
print(a, ", ", b)
# 1, 10

3.4.3 Allowed variable names

  • Unicode names can be used. This is an interesting Julia feature.
μ = 0
print(μ)

σ = 1
print(σ)

α = 0.1
print(α)

β = 10.0
print(β)
  • Built-in constants or functions cannot be replaced after being used.
pi; pi = 3
# ERROR: cannot assign a value to variable MathConstants.pi from module Main
# Stacktrace:
#  [1] top-level scope at REPL[95]:1
sqrt(4); sqrt = 4
# ERROR: cannot assign a value to variable Base.sqrt from module Main
# Stacktrace:
#  [1] top-level scope at REPL[97]:1
  • Built-in keywords cannot be modify.
else = false
# ERROR: syntax: unexpected "else"
# Stacktrace:
#  [1] top-level scope at none:1
  • Some Julia naming conventions:
    • Use lower case for variables, functions, and macros (e.g. name = "Julia").
    • Underscore (_) use is discouraged (e.g. lastname = "Montalvan").
    • Use uper camel case for Types and Modules.
    • Use ! at the end of a function name when it mutates its arguments.