Basic Julia Syntax
The syntax of a language, programming or otherwise, is the set of rules that determine whether a statement in the language is well-formed. For a language like English, syntax includes the rules of grammar. For a programming language like Julia, syntax covers how to create a command, how to call a function, and how to create a comment.
Consider the following code. Don’t worry about what it does for now; just examine how the code is structured.
# create a string
name = "Julia"
# print if the string is "Julia"
for i = 1:5
j = i * (2 + 4)
println(j)
println("Julia")
end
6 Julia 12 Julia 18 Julia 24 Julia 30 Julia
Statements
A statement in a programming language is a line of code that directs the computer to complete an action. That action can be setting the value of a variable, loading a file, printing a string, or any number of other things.
Every programming language needs a method to determine when a statement is complete. In Julia, statements end at the end of a line. So in our code,
name = "Julia"
"Julia"
is a full statement, setting the value of the variable name to "Julia."
Statements are often made up of expressions, which are any set of symbols that can be evaluated to determine their value. For instance, "2 + 3" is an expression that, when evaluated, has a value of 5. But since "2 + 3" doesn’t direct Julia to take any action with that value, it is not a full statement.
A statement that contains an expression can be broken up over multiple lines if the expression is left incomplete at the end of the first line. For example:
x = 2 +
3
5
Blocks
Sometimes a set of statements have to be grouped together in a block. For instance, we may want a certain set of statements to be repeated until a condition is met (a loop), or we may want to define a set of statements with a special name that we can use to call them whenever we need them (a function). (Both loops and functions will be covered in more detail later on.)
As with individual statements, programming languages need a method by which to determine when a block begins and when it ends. In Julia, we know that a block has begun when a special trigger word is used. We know that a block has ended when the word "end" is used. For instance, in our code:
for i = 1:5
println(i)
println("Julia")
end
1 Julia 2 Julia 3 Julia 4 Julia 5 Julia
The word "for" tells Julia that we’ve entered a kind of block called a for-loop. The word "end" tells Julia that the block is over. There are many other kinds of blocks, including while-loops, if-then statements, and functions, which we will cover later in the tutorial.
A Note on Indentation and Whitespace<
In our example block, the included statements are indented. In some other programming languages, like Python, indentation is syntactically important; statements have to be indented by the same amount for Python to know that they are part of the same block.
In Julia, indentation is not syntactically meaningful. We can create the same for-loop block without indentation and receive the same result:
for i = 1:5
println(i)
println("Julia")
end
1 Julia 2 Julia 3 Julia 4 Julia 5 Julia
However, in order for code to be easily read and debugged by users, it is still best practice to indent the statements within a block.
Similarly, whitespace within and between expressions does not syntactically matter to Julia. The following two statements, for instance, do the same thing:
x = 2 + 3
5
x=2+3
5
However, code is more readable when spaces are used between symbols, so it is best practice to include whitespace in your statements.
Comments
A comment is a line in a program that is ignored by the programming language. Comments are used to make programs more easily interpreted by human users. Often, comments are used before blocks to describe what the block does, or beside variables to document what the variable represents. They may also contain metadata about the code, such as who authored it and to what organization it belongs.
In our code, we have two comments, both of which describe what the program is about to do:
# create a string
# print if the string is "Julia"
The pound symbol (#) tells Julia that everything between that symbol and the end of the line is a comment and should be ignored. Because of that, we can also include comments on the same line as a code statement:
x = 1 + 2 # x should equal 3
3
Sometimes comments can become quite long when they describe complicated functions. In that case, we can make a multiline comment using the "#=" and "=#" symbols. The first symbol tells Julia that everything that comes before the second symbol is a comment.
#= x should equal 3
author: Madelyn Glymour
=#
x = 1 + 2
3
Parentheses
In Julia, parentheses are used for two purposes. First, they are used in a way that you may be familiar with from mathematics: to define scope in an expression. For instance, in our code:
i = 2
j = i * (2 + 4)
12
Here, the parentheses tell Julia that "2 + 4" should be evaluated first in the expression "i * (2 + 4)." If they were not there, the expression would instead be evaluated as 8:
i = 2
j = i * 2 + 4
8
Second, parentheses are used to call functions. As discussed above, a function is a block of statements that we’ve given a special name so that we can call it repeatedly. Functions often require arguments, which are user-specified values that the statements inside the function will use. To call a function, we write its name, followed by a set of parentheses, inside of which we put our arguments. For instance, in our code:
println("Julia")
Julia
println is a function from the basic Julia library that prints a string followed by a newline character. "Julia" is the argument that we’ve provided to tell the program what to print.
A function may have no arguments, in which case we still need the parentheses, with nothing inside. For instance, the println function can be called without arguments in order to print a blank line:
println()
Note that, although whitespace within expressions does not syntactically matter, whitespace between a function name and the following parentheses does matter, and will make your function ill-formed:
println ()
syntax: space before "(" not allowed in "println (" at In[22]:1
Stacktrace:
[1] top-level scope
@ In[22]:1
Summary
Statements direct Julia to take an action, and end with the end of the line.
Blocks are units of statements that have been grouped together. Blocks begin with a keyword and finish with the word "end."
Parentheses define the scope of an expression, and are used to call functions.
Comments are lines in a program that Julia doesn’t run. Single-line comments begin with the # symbol and end at the end of the line. Block comments begin with "#=" and end with "=#".
Except when it comes between a function call and its parentheses, whitespace is not syntactically meaningful, but it’s still good practice to include indentation and whitespace in your code.
