# What is the basic structure of a Julia Program?

**URL:** <https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 2, 2019, 4:02pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626 "2019-11-02T16:02:09Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sara\_J](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@sara\_J](https://discourse.julialang.org/u/sara_J)\
**Post date:** [November 2, 2019, 4:02pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/1 "2019-11-02T16:02:09Z")

</div>

I am new to Julia language but i need to know the basic structure of a Julia Program?  
I asked in stack-overflow but they are not sure.  
in c and java they have but in Julia I didn’t find and I search a lot

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [November 2, 2019, 4:08pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/2 "2019-11-02T16:08:15Z")

</div>

Julia doesn’t impose much in the way of structure for programs to run; for instance you can write

```julia
println("Hello World")

```

and save it in a file called `test.jl`. You can then run it by calling `julia test.jl` in the command line and it will print out as expected.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [November 2, 2019, 4:08pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/3 "2019-11-02T16:08:31Z")

</div>

Reference: [What is the basic structure of a Julia program? - Stack Overflow](https://stackoverflow.com/questions/58646668/what-is-the-basic-structure-of-a-julia-program)

The original question was I believe [https://stackoverflow.com/questions/58645164/what-is-the-basic-structure-of-julia](https://stackoverflow.com/questions/58645164/what-is-the-basic-structure-of-julia), now deleted?

---

<div class="post-metadata">

**Author:** ![jbmuir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbmuir/32/4798_2.png) [@jbmuir](https://discourse.julialang.org/u/jbmuir)\
**Post date:** [November 2, 2019, 4:12pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/4 "2019-11-02T16:12:06Z")

</div>

For more efficient and maintainable programs though, it is best to encapsulate program behaviour inside functions which you call - for instance you could emulate a typical C-like format by doing something like the following

```julia
function f(x,y)
   return x+y
end

function main()
    x = 2
    y = 3
    println(f(x,y))
end

main()
```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [November 2, 2019, 5:40pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/5 "2019-11-02T17:40:29Z")

</div>

Building on jbmuir’s answer, I usually define a top-level file that calls packages and my own custom code. For example,

```julia
#main.jl
using some_package, another_package
#A file with functions and structs etc. 
include("custom_functions.jl")
#You can keep your code in a module
push!(LOAD_PATH,path_to_my_module.jl)
using my_module
inputs = ...
results = main(inputs)
#some processing below

```

```julia
#custom_code.jl
function myfunction()
...
end

```

In this example, there is a main file that calls packages and custom code, and you can perform some task/analysis and process the results. You can organize your supporting code into files based on function. If you find that you are reusing code a lot, it might be useful to organize it as a local [package](https://www.youtube.com/watch?v=QVmU29rCjaA&t=1412s). If you go that route, l recommend looking through an established package in your area of expertise. For example, if you perform Bayesian parameter estimation, you could look at [DynamicHMC](https://github.com/tpapp/DynamicHMC.jl).

As the others noted, there are not many constraints imposed. What is important is that your code is organized, modular and reusable. So basically just exercise good practice.

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [November 2, 2019, 5:58pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/6 "2019-11-02T17:58:37Z")

</div>

For a newbie, the link below may also be useful. One needn’t understand or appreciate everything in the link yet but I found it useful to keep coming back to it from time to time when I was starting to experiment with Julia.

> **[7 Julia Gotchas and How to Handle Them - Stochastic Lifestyle](http://www.stochasticlifestyle.com/7-julia-gotchas-handle/)**
>
> Let me start by saying Julia is a great language. I love the language, it is what I find to be the most powerful and intuitive language that I have ever used. It’s undoubtedly my favorite language. That said, there are some “gotchas”, tricky little...

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [November 3, 2019, 2:57am UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/7 "2019-11-03T02:57:28Z")

</div>

bashonubuntu,

[http://www.stochasticlifestyle.com/7-julia-gotchas-handle/](http://www.stochasticlifestyle.com/7-julia-gotchas-handle/)

has the best explanation on why global variables are consider harmful (in Julia)

Thank you for bring it to my attention.

---

<div class="post-metadata">

**Author:** ![ElectronicTeaCup](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/electronicteacup/32/12647_2.png) [@ElectronicTeaCup](https://discourse.julialang.org/u/ElectronicTeaCup)\
**Post date:** [May 21, 2020, 9:14am UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/8 "2020-05-21T09:14:30Z")

</div>

Is this generally a good way to go about writing a script? My script currently looks something like this

```julia
sub-function_1
sub-function_2
sub-function_3

<load files>

<do analysis using sub-functions >

<produce plots>

```

So the latter part I should wrap up into a main() function? Is it terrible practice not to place the sub-functions at the top, but in-line where they are used? I’m having difficulty in scrolling up every time I need to see what a function is doing.

Either this or what @Christopher_Fisher did with their `include("custom_functions.jl")` line. What are your thoughts now? Still doing the same thing or are there better ways of organizing code which uses functions/sub-functions on in a single script/program.

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [May 21, 2020, 1:04pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/9 "2020-05-21T13:04:22Z")

</div>

As long as the actual heavy work is done inside functions, and those functions don’t refer to global variables, I don’t think it is necessary (performance-wise) to wrap everything in a `main()` function. But maybe someone will correct me on this.

In my opinion it is best practice to separate function definitions from code that does actual work as much as possible. Even better I think is to put all your functions inside a package and include tests (maybe it doesn’t always make sense for quick exploratory analysis, but at some point it is good to start organizing).  
Maybe you will find [DrWatson.jl](https://github.com/JuliaDynamics/DrWatson.jl) useful with regards to project organization best-practices.

> [@ElectronicTeaCup](#):
>
> I’m having difficulty in scrolling up every time I need to see what a function is doing.

Probably your editor (together with a Julia plugin) has some kind of “jump to definition” functionality which allows you to jump to the function definition and back, so the actual location of the function shouldn’t be so important if you have this feature.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [May 21, 2020, 4:47pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/10 "2020-05-21T16:47:35Z")

</div>

I agree with orialb. At the very least, it is good practice to store functions in a separate file or module and call them from a script. It reduces clutter and allows reuse of your code. I also agree that putting your functions in a package can be a good idea, especially if you use the functions across multiple projects. You can create tests and conveniently call your package without needing to specify paths. I also recommend creating project specific environments so that you can restrict your dependencies to versions known to work. If you want to update your dependencies, you can verify that new versions work. Running code months later only for it to crash is the worst.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 22, 2020, 8:33am UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/11 "2020-05-22T08:33:04Z")

</div>

> [@ElectronicTeaCup](#):
>
> Is it terrible practice not to place the sub-functions at the top, but in-line where they are used? I’m having difficulty in scrolling up every time I need to see what a function is doing.

This really depends on your personal preferences, the way you navigate code, and the code length. Julia can handle whatever you prefer.

My normal workflow is to start out with a small script, wait for it to inevitably evolve into utter chaos, make a half-hearted attempt at moving the functions to the top, then realize that I should have done a project to begin with because then I can use Revise.

---

<div class="post-metadata">

**Author:** ![Markus\_Hauschel](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Markus\_Hauschel](https://discourse.julialang.org/u/Markus_Hauschel)\
**Post date:** [May 30, 2020, 10:03pm UTC](https://discourse.julialang.org/t/what-is-the-basic-structure-of-a-julia-program/30626/12 "2020-05-30T22:03:24Z")

</div>

The most important thing to me is `using Revise`.
