# Code organisation: Move functions to the end of the code

**URL:** <https://discourse.julialang.org/t/code-organisation-move-functions-to-the-end-of-the-code/93139>\
**Category:** New to Julia\
**Created:** [January 18, 2023, 9:14am UTC](https://discourse.julialang.org/t/code-organisation-move-functions-to-the-end-of-the-code/93139 "2023-01-18T09:14:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)\
**Post date:** [January 18, 2023, 9:14am UTC](https://discourse.julialang.org/t/code-organisation-move-functions-to-the-end-of-the-code/93139/1 "2023-01-18T09:14:18Z")

</div>

Hi all,

I wonder if I aim to code based on a single \*.jl (without any external modules etc) if I could move the function definitions at the very low end of my script. Coming from Fortan I used to work like that which made my code appear much cleaner. Can I achieve a similiar behavior in Julia ? So that say the compilation order is changed ?

MWE:

```julia
# Input
x = 10.0;
y = 20.0;

# Output
zsum = mysum(x,y);
zprod = myprod(x,y);
println("sum:", zsum);
println("prod:", zprod);

#############################
# Collected Functions
function mysum(a,b)
    c = a+b
end 

function myprod(a,b)
    c = a*b
end

```

Thank you in advance for your support.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 18, 2023, 9:26am UTC](https://discourse.julialang.org/t/code-organisation-move-functions-to-the-end-of-the-code/93139/2 "2023-01-18T09:26:29Z")

</div>

I don’t think you can, although Pluto notebooks work that way - they have n execution order of cells which is independent of the display order, and many people use this to do exactly what you describe here.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [January 19, 2023, 12:07am UTC](https://discourse.julialang.org/t/code-organisation-move-functions-to-the-end-of-the-code/93139/3 "2023-01-19T00:07:41Z")

</div>

Code is evaluated sequentially, so functions need to be defined before they are called.

Here are some alternatives:

1. Place your functions in another file and `include` that file at the start of your script:

2. Place your script in a function and call the function at the end of the file (or from the REPL after `include`ing the file):

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [January 19, 2023, 7:13am UTC](https://discourse.julialang.org/t/code-organisation-move-functions-to-the-end-of-the-code/93139/4 "2023-01-19T07:13:11Z")

</div>

@ceysa75 You can use Stevens solution and then try this:

> [@Is this a good way to export all variables inside of a function?](https://discourse.julialang.org/t/is-this-a-good-way-to-export-all-variables-inside-of-a-function/73699):
>
> Hello! Not a question per se, more looking for some feedback I suppose. So by googling a bit, I came around and saw that it is possible to call a macro called Base.@locals, which if you use it inside of a function, you can make it return all variables inside of the function as such: return Base.@locals I have a need for this right now (or atleast I think I do), is there something I should be aware about when using this? Kind regards

This should make it possible to develop in a script, getting the performance boost of a function and then being able to extract everything from the function easily.

Kind regards
