Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to change the argument of a function?

226 views
Skip to first unread message

Vilis Nams

unread,
Feb 4, 1998, 3:00:00 AM2/4/98
to

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
-------------------

Allan Hayes

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

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:

Could you explain more fully what you wish to do please? I'm not sure
what "changes the value of its argument" means. The trouble with

In[1]:=


f[x_]:=Module[{},
x=2*x;
x
]

In[2]:=
f[2]

>From In[2]:=
Set::setraw: Cannot assign to raw object 2.

Out[2]=
2

is that every x on the right is replaced by 2, so that Mathematica is
asked to evaluate Module[{}, 2 = 2 2, 2], and can't assign to 2.

But,

f[x_] := 2 x

does what you seem to have been aiming.

Allan Hayes
Training and Consulting
Leicester, UK
h...@haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642

Jean-Marie THOMAS

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

Try:
f[x_]:=x+1;
and set the new value for x:
x=f[x]

Comment: is it the old problem in Pascal or C: passing a parameter by
address or by value? I am not sure of this, but I feel Mathematica
copies all the input parameters, but keeps for these the same name.

Hope this helps,
-----------------------------------------------

Jean-Marie THOMAS
Conseil et Audit en Ingenierie de Calcul jmth...@cybercable.tm.fr
+33 (0)3 88 32 93 64
www.cybercable.tm.fr/~jmthomas
=======================


-----Message d'origine-----
De: Vilis Nams [SMTP:vn...@nsac.ns.ca] Date: jeudi 5 fevrier 1998 06:58
A: math...@smc.vnet.net
Objet: [mg10783] How to change the argument of a function?

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?

sean...@worldnet.att.net

unread,
Feb 11, 1998, 3:00:00 AM2/11/98
to

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.


Stonewall Ballard

unread,
Feb 12, 1998, 3:00:00 AM2/12/98
to

In article <6brkhm$lqs$3...@dragonfly.wolfram.com>,
sean...@worldnet.att.net wrote:

This is a classic programming problem, solved by using "reference" args
or "call by name". The example used in most cases is swap[x, y], which
swaps the values of the arguments.

In Mathematica it can be done like:

swap[x_, y_] := Module[{t = x}, x = y; y = t;]

SetAttributes[swap, HoldAll]

then try:

foo = 1
1
bar = 5
5
swap[foo, bar]

foo
5
bar
1

- Stoney

--
Stonewall Ballard StoneSoft, Inc.
sb.n...@stonesoft.com http://www.stonesoft.com/


j...@max.mpae.gwdg.de

unread,
Feb 14, 1998, 3:00:00 AM2/14/98
to

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

Allan Hayes

unread,
Feb 15, 1998, 3:00:00 AM2/15/98
to

Stonewall Ballard wrote:
>
> In article <6brkhm$lqs$3...@dragonfly.wolfram.com>,
> sean...@worldnet.att.net wrote:
>
> This is a classic programming problem, solved by using "reference" args
> or "call by name". The example used in most cases is swap[x, y], which
> swaps the values of the arguments.
>
> In Mathematica it can be done like:
>
> swap[x_, y_] := Module[{t = x}, x = y; y = t;]
>
> SetAttributes[swap, HoldAll]
>
> then try:
>
> foo = 1
> 1
> bar = 5
> 5
> swap[foo, bar]
>
> foo
> 5
> bar
> 1
>
> - Stoney
>
> --
> Stonewall Ballard StoneSoft, Inc.
> sb.n...@stonesoft.com http://www.stonesoft.com/


Or

In[1]:=
$Line=0;

In[1]:=
SetAttributes[swap, HoldAll]
swap[x_, y_] := ({x,y} = {y,x};)

In[3]:=
{foo, bar} = {1,5}

Out[3]=
{1, 5}

In[4]:=
swap[foo,bar]

In[5]:=
{foo,bar}

Out[5]=
{5, 1}


--

0 new messages