Storing functions for usage across files

I came from MATLAB and have enjoyed the migration to Julia. However, one thing that I liked about MATLAB was the ability to create a function and save that function in a directory, so that I could use it later in a potentially different project. That saved many lines of code.

So, my simple question is this: Is it possible in Julia to save a function in a particular directory, and use that function across different files, without having to introduce the entire function code every time?

1 Like

Yes, you can always just do include("myfile.jl") if that file contains your function definitions.

The more elaborate version of this is to turn whatever functions you have into a package that you then load in the normal way doing using MyFunctions

1 Like

That is nice. So, if I understand correctly, I can create a “function depository file”, and refer to that file in projects using “include”?

Yes, you can do that, but you should not have using instructions in that file. If you want to have using instructions and functions together you should create your own package.

Julia is vastly better at this than Matlab, thanks to Julia’s built-in packaging system (integrated with dependency management, unit testing, version control, and other niceties).

See also the discussion: Best practise: organising code in Julia - #2 by stevengj

3 Likes

Just to add to stevengj’s answer, I make use of PkgTemplates.jl to easily create new sharable projects with the folder structure, git repository, and CI all set up with a single command. I just have a new_project function in my startup file (so I can use it anywhere) that creates the template to my liking. See saving templates for some pointers in this direction.

1 Like

Well, for a beginner that is really overkill. I created my first package 4 years after starting to use Julia…

2 Likes

I agree, it’s quite a steep learning curve, but I think it’s worth being aware of at least so you can come back to it once you’ve got the basics, even if you don’t use it immediately.

I’ve had quite a few of my students waste a lot of time trying to figure out why their code is no longer working when they come back to it after a few months. If you start using projects early, as well as the code organisation benefits, you save yourself that pain because all the package versions are recorded and so you know your code will continue to work even if the packages you use make breaking changes because the package manager will use the right versions for you. (Admittedly this was work in the SciML ecosystem which is rapidly developing, probably more so than other areas.)

PkgTemplates makes it relatively painless - any time I start a new piece of work it’s just a single line to set up a new project. For my students I made a short video on how to do it.

But yes, it is another thing to learn and might not be needed immediately…

2 Likes

Well, creating a project does not mean to create a package. It is much simpler:

mkdir test
cd test
mkdir src
julia --project="."

Now add the packages you need:

using Pkg
pkg"add Plots"
pkg"add DataFrames"

Now put your code in a file in the src folder, for example like this:

cd src
gedit hello_world.jl

and put the following code into it:

println("Hello world!")

and save it.
If you want to run it, make sure you are in the test folder and then
start julia with:

julia --project

and execute your script with:

include("src/hello_world.jl")

When you are happy with your code and the packages you are using, make a backup copy
of your Manifest.toml file:

cp Manifest.toml Manifest.toml.bak

If you - half a year later - update your packages and your code stops to work, just restore the Manifest file:

cp Manifest.toml.bak Manifest.toml

No need to create any module or Julia package…

3 Likes