I have the following code
Module[{f,x=0},
f[t_] := x + t;
..........;
...f[1]...;
..........;
]
My aim is to put the declaration of the function f
outside the module.
Passing by reference:
Attributes[f] = HoldFirst;
f[x_Symbol, t_] := x + t;
Module[{x = 0},
f[x, 1]
]
Ref: http://dev.ragfield.com/2009/03/mathematica-call-by-reference.html
I just found an alternative small trick, which imitates the declaration of an inline function and solves the question:
f[x_Symbol, t_] := x + t;
temp = Hold[Module[{x = 0},
f[x, 1]
]] /. DownValues[f]
ReleaseHold[temp]
f
as aModule
variable? $\endgroup$x
tof
as a parameter. $\endgroup$x
tof
, you could use currying: declare a global functionmakeF
that takes a parameterx
and returns a new functionf
. Then usef=makeF[x];
inside the module where you want to usef
. $\endgroup$x
anyway even with the attributes when you doxx+t
, even if you save it back toxx
, so in that regard you won't save time. But it's true that you need the attribute to pass by reference and changex
from insidef
$\endgroup$Part[xx,1]
and process that further), no copy is created, right? In other words, just the fact that you pass an expression by-value does not mean you use more memory... $\endgroup$