Variables, Expressions, and Statements in Julia
3 min read
In this article, I'd be explaining the following concepts in the Julia programming language: Variables, Expressions, and Statements.
P.S: If you have no idea of what Julia is, you can read up here.
Let's dive in...
What are Variables?
Variables can be seen as containers used to store data that the program can use over time and can be called at any point in the codebase. Unlike C and Java, variables in Julia need not to be written with a Datatype. It auto-assigns the data type automatically like Python.
Using the Julia REPL,
julia> message = "Hello World" "Hello World"
One of the features of a powerful language is the ability to manipulate variables. In Julia, a variable can be overwritten (the content of a variable replaced with a new one). Using the previous example,
julia> message = "Hello World" "Hello World" julia> message = "I love Julia" "I love Julia"
see how the message was overwritten
Variables can be given any name, as long as it's meaningful to the codebase. Some of the rules that apply in the naming convention in Python applies here. They include:
- A variable cannot start with a number
julia> 7letters = "some text" ERROR: syntax: "7" is not a valid function argument name
- A variable can start with uppercase, but it's conventional to begin variables with lower cases.
- For variables with long names, you use the underscore character "_". Leaving a space would give an error
julia> your name = "Julia" ERROR: syntax: extra token "name" after end of expression
- Keywords cannot be used as variable names in Julia. For example, struct is a keyword in Julia
julia> struct = "Exploration" ERROR: syntax: unexpected "="
The keywords in Julia are:
You do not need to memorize them. Keywords are displayed in a different color in most development environments.
What is a Statement?
A statement is a piece of code that performs a specific task, such as creating a variable or displaying a value. The assignment of a value to a variable is written in a statement. An assignment statement creates a new variable and gives it a value
julia> note = "random words" "random words"
Displaying the value of a variable is also done with a statement. From the previous example,
julia> println(note) random words
A combination of values, variables, and operators is called an expression. A variable, like a value, is regarded an expression by itself. Below is a legal representation of an expression
julia> 37 37 julia> n = 10 10 julia> n + 25 35
When you type in an expression in the REPL, it gets evaluated immediately. For example,
n has the value 10 and
n + 25 has the value 35.
Global vs Local Variables
In Julia, variables can be assigned globally or locally. A global variable is a variable that can be used throughout the program while a local variable is a variable that is declared in a function and given a local scope.
Julia uses the
global inbuilt function to declare global variables. For example
global b = 4 function addNumber(a) return a + b end println(addNumber(3))
The result of this is
Like most other programming languages, Julia makes provision for creating variables, statements, and expressions, which make writing readable and portable code easier. Getting the hang of how it is used is essential for all developers.