Minimizing Work in Copying/Manipulating Custom Data Structures

So the following two versions both do what I had expected. I’m originally from the MATLAB world where it would be quite natural to have the essential loop inside of scripting file that you would call. I guess the paradigm for Julia is to put everything inside of functions?

struct DataStruct{TF<:AbstractFloat, TI<:Integer}
   X::Vector{TF}
   Y::Vector{TI}
end

function struct_func!(D_work::DataStruct, D::DataStruct)
   n = length(D.X);

   for (j,k) in zip(1:n,n:-1:1)
      D_work.X[j] = sin(D.X[j] * (D.Y[k]));
      D_work.Y[j] = D.Y[j] - Int(j);
   end
   D_work, D
end

function iterate_func!(D::DataStruct,D_work::DataStruct, n)

   for j in 1:n
      D, D_work = struct_func!(D_work,D);
   end
   D, D_work
end

m = 5
S = DataStruct{Float64, Int}(range(0,stop=1, length=m), 2*ones(m));
S_work = deepcopy(S);
T = deepcopy(S);
T_work = deepcopy(S);

N = 4;

for j in 1:N
   global S, S_work = struct_func!(S_work,S);
end
iterate_func!(T, T_work, N);