Julia Programming - Basic Syntax



The simplest first Julia program (and of many other programming languages too) is to print hello world. The script is as follows −

hello world

If you have added Julia to your path, the same script can be saved in a file say hello.jl and can be run by typing Julia hello.jl at command prompt. Alternatively the same can also be run from Julia REPL by typing include(“hello.jl”). This command will evaluate all valid expressions and return the last output.

Variables

What can be the simplest definition of a computer program? The simplest one may be that a computer program is a series of instructions to be executed on a variety of data.

Here the data can be the name of a person, place, the house number of a person, or even a list of things you have made. In computer programming, when we need to label such information, we give it a name (say A) and call it a variable. In this sense, we can say that a variable is a box containing data.

Let us see how we can assign data to a variable. It is quite simple, just type it. For example,

student_name = “Ram”
roll_no = 15
marks_math = 9.5

Here, the first variable i.e. student_name contains a string, the second variable i.e. roll_no contains a number, and the third variable i.e. marks_math contains a floating-point number. We see, unlike other programming languages such as C++, Python, etc.,in Julia we do not have to specify the type of variables because it can infer the type of object on the right side of the equal sign.

Stylistic Conventions and Allowed Variable Names

Following are some conventions used for variables names −

  • The names of the variables in Julia are case sensitive. So, the variables student_name and Student_name would not be same.

  • The names of the variables in Julia should always start with a letter and after that we can use anything like digits, letters, underscores, etc.

  • In Julia, generally lower-case letter is used with multiple words separated by an underscore.

  • We should use clear, short, and to the point names for variables.

  • Some of the valid Julia variable names are student_name, roll_no, speed, current_time.

Comments

Writing comments in Julia is quite same as Python. Based on the usage, comments are of two types −

Single Line Comments

In Julia, the single line comments start with the symbol of # (hashtag) and it lasts till the end of that line. Suppose if your comment exceeds one line then you should put a # symbol on the next line also and can continue the comment. Given below is the code snippet showing single line comment −

Example

julia> #This is an example to demonstrate the single lined comments.
julia> #Print the given name

Multi-line Comments

In Julia, the multi-line comment is a piece of text, like single line comment, but it is enclosed in a delimiter #= on the start of the comment and enclosed in a delimiter =# on the end of the comment. Given below is the code snippet showing multi-line comment −

Example

julia> #= This is an example to demonstrate the multi-line comments that tells us about tutorialspoint.com. At this website you can browse the best resource for Online Education.=#
julia> print(www.tutorialspoint.com)
Advertisements