Structure and performance questions from a Fortran programmer: functions, global variables, array allocations, input files, and structs

@ducksoverip I’m in the same boat as you, except that I started a few years earlier than you. So, my experience might be of some help to you. Instead of answering all your questions, I’ll answer the easiest one (to me) first:

First of all, the struct is less “necessary” in Julia than in most other languages including F90, C, C++, Java, etc.

In a fully object oriented programming style, the struct (or “class”) is essential to bundle multiple variables to treat them as a single object. In a not fully object oriented style, bundling multiple variables can be useful, for example, if you want to handle a point in a 3D space as a single object:

type Point
    real:: x, y, z
end type Point
type(Point):: p
! use p.x, p.y, p.z

In other languages than Julia, you may also want to bundle “a bunch of parameters to pass to a function”, as you say, but in Julia, the NamedTuple is more flexible and easier to use:

function myfunc(; x, y, z)
   # . . . use x, y, z and produce a, b, c
   a = . . . 
   b = . . .
   c = . . . 
   return (; a, b, c)
end

# call myfunc with an explicit bundle
pars = (; x = 3.14, y = 2.0, z = -999.9) # NamedTuple
ret = myfunc(; pars...) # "expand" it as arguments
# . . . use ret.a, ret.b, ret.c

# call myfunc with implicit NamedTuple
ret = myfunc(; x = 3.14, y = 2.0, z = -999.9)
# . . . use ret.a, ret.b, ret.c

# Same, except the returned NamedTuple is expanded:
(; a, b, c) = myfunc(; x = 3.14, y = 2.0, z = -999.9)
# . . . use a, b, c

NamedTuple is a light-weight mechanism to create a struct-like object. It can be used like a struct: println(pars.x), for example. Yet, unlike the real struct, it doesn’t need a formal definition such as type Point; real:: x, y, z; end type Point.

Moreover, you can choose between expanding it or not doing so each time you use the function: You don’t force the user of your function to formally create a struct.

So, I recommend using structs only where you would use structs in Fortran. In other situations, you can just use NamedTuples.