Dynamically assign variables in a function

I would like to assign variables in functions. For example,

function func(p)
a = A(p)
b = B(p)
c = C(p)
# Then do something using a, b, c
end

Those a, b, c are parameters I want to use in the function, and they depend on another parameter p.
The same parameters are used in several functions, and I don’t want to repeat the same assignment in all the functions.
It seems that I can use a macro with esc() to replace the assignment statement, but is therer a better way to do this?

Thank you.

If you need to repeat several calculations like this in a bunch of places, then you should refactor it as a function:

a,b,c = ABC(p)

If you want to perform the calculation once and then re-use the values a,b,c in many functions, then you should pass them as parameters to those functions. If you are always passing a,b,c together, that might be a hint that you should collect them into struct or some other data structure.

2 Likes

I need to recalculate them according to p.
Thanks for your suggestions.