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.
10
x = ", ", typeof(x))
print(x, # 10, Int64
10.0
x = ", ", typeof(x))
print(x, # 10.0, Float64
"Hello World!"
x = ", ", typeof(x))
print(x, # Hello World!, String
1 + 2im
x = ", ", typeof(x))
print(x, # 1 + 2im, Complex{Int64}
1 // 2
x = ", ", typeof(x))
print(x, # 1//2, Rational{Int64}
3.4.2 Define more than one variable
More that one variable can be defined using tuples syntax.
1, 10)
(a, b) = (", ", b)
print(a, # 1, 10
1, 10
a, b = ", ", b)
print(a, # 1, 10
1, 10
(a, b) = ", ", b)
print(a, # 1, 10
1, 10)
a, b = (", ", b)
print(a, # 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.
3
pi; pi = # ERROR: cannot assign a value to variable MathConstants.pi from module Main
# Stacktrace:
# [1] top-level scope at REPL[95]:1
4); sqrt = 4
sqrt(# 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
andModules
. - Use
!
at the end of a function name when it mutates its arguments.
- Use lower case for variables, functions, and macros (e.g.