MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: How to change the argument of a function?



Hi, 

all that answer the f[x_]:=2x problem. Did anybody read the manual or
the ``Programming in Mathematica'' book by Roman M"ader ?

The correct solution is

SetAttributes[f,HoldAll]
f[x_]:=x=2*x


Than You can write

x=1;
f[x];
x

and get the expected 

Out[]= 2

This answer where posted several times in the  news group.

Hope that helps
  Jens


> Vilis Nams wrote:
> > 
> > I want to define a function that in the course of doing something, also
> > changes the value of its argument, for example: f[x_]:=Module[{},
> > x=2*x;
> > x
> > ]
> > When I run the function, I get the error: "Set::setraw : Cannot assign
> > to raw object  ...." Can this be done in some way?
> > 
> > --------------------
> > Vilis O. Nams
> > Dept of Biology, NSAC
> > Box 550, Truro, NS,  Canada
> > vnams @ nsac.ns.ca
> > -------------------
> 
> 
> If I have an argument I want to change in the course of a functional
> evaluation, I make a "working" copy at the beginning of the function:
> 
> f[x_]:=Module[{y},
>   y=x;
> > y=2*y;
> > y
> > ]
> 
> I don't think you can change the value of the actual argument inside a
> function, or if you could that it would be a good idea.  If you made
> global changes inside a local function, it might make for some
> interesting bugs that would be very hard to track down. -- 
> Remove the _nospam_ in the return address to respond.
> 



  • Prev by Date: Re: Active Plots
  • Next by Date: Re: question on ErrorBar
  • Prev by thread: RE: How to change the argument of a function?
  • Next by thread: Q: Contour integral