Hello, I am new to programming in Julia and currently working on transitioning code from Fortran to Julia.
In a file called codeA.jl, I have declared several variables, some of which are utilized as parameters within a function. For example:
a = 0.0
b = zeros(Float64, a)
function f(x::Int)
c = b.*x .+ a
return c
end
This code from codeA.jl is then included in another file called codeB.jl.
In codeB.jl, the values of the a and b variables from codeA.jl are modified, and I call the function f within codeB.jl.
In an effort to reduce the number of allocations, following recommendations I found in this forum, I included the variables a and b as fixed inputs to f. This means that I modified f to be defined as:
a = 0.0
b = zeros(Float64, a)
function f(x::Int, av = a, bv = b)
c = bv.*x .+ av
return c
end
After this modification, the number of allocations decreases almost to zero. However, if I change the values of a and b (from codeA.jl) in codeB.jl, the values of a and b are not updated when I call f in codeB.jl. This raises the following questions:
(1) Is this approach of fixing a and b as variables of f a natural solution to reduce the number of allocations? I need to call f several times in codeB.jl (inside a loop), so I prefer to define a and b outside the f function. Additionally, I prefer not to specify a and b when calling f (e.g., f(x, a, b)), as only x is meaningful in the problem modeled by f.
(2) In the way I have modeled this problem, how can I ensure that when I call the f function in codeB.jl, it recognizes the new values of a and b?
(3) Given the conditions outlined in (1), is there a better or more natural way to organize the code?