Below is a small demo program that is working. The idea I am exploring is for a function (main in this case) to call a function (data in this case) that generates some data c and d. This data is then passed back to main and then sent to print_data for printing. Questions:
You will note in the function data(j) there is a line parameters = data_generated(c,d). I am not sure why I need the name (in this case data_generated) in front of (c,d) to define parameters?
I thought I needed to define data_generated as a “struct”, but even with the struct commented out, the program still works.
I must be missing something very fundamental. Hope someone can help. Once I get these principles established I can apply to my much bigger problem
Thanks again kind folks. Peter
# struct data_generated
# c::Int64
# d::Int64
# end
function data(j)
# test function to generate some data and pass back to main
c=100+j
d=200+j
parameters = data_generated(c,d)
return parameters
end
function print_data(parameters)
# data coming from main to print
@unpack c, d = parameters # unpack parameters
@show c, d # print data
end
function main()
for j in 1:5
parameters = data(j) # call function that generates data
print_data(parameters) # print results
end
end
main()
(c, d) = (101, 201)
(c, d) = (102, 202)
(c, d) = (103, 203)
(c, d) = (104, 204)
(c, d) = (105, 205)
Please make sure that you produce an actual MWE (see here for an explanation) that shows your problem. The code you posted does not actually run:
julia> main()
ERROR: UndefVarError: data_generated not defined
(in addition, there’s an error @unpack not defined, because you didn’t include using Parameters in your example).
I’m assuming that you just commented out the struct definition and re-ran the code without re-starting your Julia session? In that case data_generated will still have been defined.
As a general tip it’s always useful once you have an MWE that you think exhibits your issue to just fire up a fresh REPL session and copy-paste it to make sure it behaves as expected.
I’m assuming that you just commented out the struct definition and re-ran the code without re-starting your Julia session? In that case data_generated will still have been defined.
Yes you are both right. I didn’t include “using Parameters” in the code I posted, although I has used it in my code. Also, after restating (with the struct commented out) the code didn’t run. After uncommenting the struct, all works well and the universe is as it should be. Thanks for your help. The journey continues.
I think you’re basically there, the above is a decent MWE (after adding the required packages), but as I said best thing to do is probably to just re-run it in a fresh session before posting it!