How can I just use the return value from one function but without calling that function in Julia

I know it maybe weird to ask this kind of question, but I am not sure Julia has this kind of way to achieve what I want. Here is one example code:

function initialize(a,b,c)
do something
return x
end

function run(a,b,c)
do something with the output from initialize, x
end

I am wondering that could I just use the return value from initialize() function, but without calling it? The reason I don’t want to call for initialize() is because my initialize() function is very time consuming, and the run() function is within a loop, if I call the initialize() function every time, it would be very time consuming. Any suggestions would be very appreciated.

Can you calculate x once and then pass it to run? Something like:

function initialize(a, b, c)
    ...
end

function run(a, b, c, x)
    ...
end

x = initialize(a, b, c)

for ...
   run(a, b, c, x)
end
1 Like

Are you looking for memoization? GitHub - JuliaCollections/Memoize.jl: @memoize macro for Julia

5 Likes