Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Passing data through function calls

$
0
0

Hi,

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:

  1. 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?
  2. 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)

6 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles