What is the basic structure of a Julia Program?

First off, if you are new to Julia and the structure of writing a program therein, I suggest you check out the Official Julia Docs for a great explanation as to how to get started writing code in Julia.

If you are coming from a language like Java and read the documentation linked above, you may be a little confused as to where the docs on the structure of a Julia program are. To my understanding, that doc does not exist for a few reasons.

Julia as a language imposes very little on you as the programmer. This can lead to a bit of uncertainty and doubt with all of the newfound freedom. There are some basic structures that should be followed, despite the flexibility the language provides:

  1. using and import statements are generally made at the very top of the file.
  2. That's about it! (Feel free to edit if you think there's more).

It's also worth checking out the Julia Style Guide for things like:

Write functions, not just scripts: Writing code as a series of steps at the top level is a quick way to get started solving a problem, but you should try to divide a program into functions as soon as possible. Functions are more reusable and testable, and clarify what steps are being done and what their inputs and outputs are. Furthermore, code inside functions tends to run much faster than the top-level code, due to how Julia's compiler works.

In general, Julia is flexible. There are very few things that you have to include in your program.

It's important to designate the difference between writing a simple Julia script and creating a project in Julia.

While there are limited structural suggestions for creating a simple script, there are a number of suggestions related to how one can structure a Julia Project. In fact, many of these aspects are built into Julia itself! You can find out more about creating Julia Projects (which should have a similar if not the same structure as Julia packages) here.

Note: If you are trying to find the structure of a Package in Julia, a great resource would be PackageTemplate.jl.


Larger Julia programs tend to first have modules that are included, then any const declaration of const global variables, then one or more functions, then a statement that calls the first function run.

However, your question may be due to the fact that you already know that C and Java have a certain structure mandated in order to compile and run.

Perhaps comparing "Hello World" minimal programs would help.

Here is a Julia minimal program:

println("Hello world!")

Julia its inherits its free program structuring more from Fortran and especially Python than C or Java. Here is a Python minimal program:

print("Hello world!")

Here is a Fortran77 minimal program:

print *, "Hello world!"

On the other hand, C requires, for the same function, a main() wrapper function and inclusion of code for printing to screen. Here is a C minimal program:

#include<stdio.h>

int main()
{
    return printf("Hello World!\n");
}

Java also requires that its main() function be wrapped within a user defined class. Here is a Java minimal program:

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello world!");
    }
}

So, Julia simply does not have certain structural requirements that you find well documented for the C or Java languages.

Tags:

Julia