Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Modifying list passed as argument' topicStudent Support Forum > General > Archives > "Modifying list passed as argument"

< Previous CommentHelp | Reply To Comment | Reply To Topic
Author Comment/Response
yehuda ben-shimol
09/24/09 02:26am

Hi,
What you need here is a "call by reference" mechanism that overpasses the ordinary evaluation process of Mathematica, which is a call by value

you can do it in two ways, the first one is preferred

SetAttributes[test, HoldAll]
test[list_] := Block[{x = 2}, list[[2]] = x];
lst = {4, 5, 6};
test[lst];
lst


OR

test1[list_] := Block[{x = 2}, list[[2]] = x];
lst = {4, 5, 6};
test1[Unevaluated[lst]];
lst

in both cases the list lst is not evaluated and passed as is to the function

yehuda



URL: ,

Subject (listing for 'Modifying list passed as argument')
Author Date Posted
Modifying list passed as argument qj 09/16/09 4:52pm
Re: Modifying list passed as argument Peter Pein 09/19/09 7:34pm
Re: Re: Modifying list passed as argument qj 09/21/09 3:17pm
Re: Modifying list passed as argument yehuda ben-s... 09/24/09 02:26am
< Previous CommentHelp | Reply To Comment | Reply To Topic