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?
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).
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.
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…