Julia Basic Syntax
Variables
Variables originate from mathematics and are abstract concepts in computer languages that can store calculation results or represent values.
Variables can be accessed by their names.
Julia language variable names consist of letters, numbers, and underscores _
, with the first character not being a number.
Variable names are case-sensitive.
Using variables in Julia is straightforward; simply assign a value to them, as shown in the example below:
Example
# Assign 10 to variable x
x = 10
# Use the value of x for calculation
x + 1
11
# Assign a string to variable site_name
site_name = "tutorialpro"
# Floating-point data
marks_math = 9.5
In the interactive command environment, the output is as follows:
From the examples, we see that unlike other programming languages such as C++ and Java, Julia does not require specifying variable types; it can automatically infer the type of the object on the right side of the equal sign.
Naming Conventions
- Use lowercase for variable names.
- Use underscores
_
to separate words in variable names. - Names of types
Type
and modulesModule
start with uppercase letters and use uppercase letters to separate words instead of underscores. - Function (
function
) and macro (macro
) names are in lowercase and do not use underscores. - Functions that modify their input parameters should end with
!
. These functions are sometimes called "mutating" or "in-place" functions because they modify the contents of their input parameters after being called, not just returning a value.
Comments
Julia supports single-line and multi-line comments.
Single-line comments in Julia start with #
, for example:
Example
# This is a single-line comment
# This is another single-line comment
println("Hello World!")
Multi-line comments are enclosed with #=
and =#
, for example:
Example
#=
1. This is a single-line comment
2. This is another single-line comment
=#
println("Hello World!")