Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'CALL by value and CALL by reference' topicStudent Support Forum > General > "CALL by value and CALL by reference"

Help | Reply To Topic
Author Comment/Response
yehuda ben-shimol
12/08/06 07:23am


Although I gave some details in my reply to
http://forums.wolfram.com/student-support/topics/12513
I have the impression that this issue worth some more discussion since many of the novice Mathematica users arrive with some "programming habits" that are adopted from other programming languages, such as c, JAVA, c++ and alike.
Formally, a call by value is identical to the default parameter passing in the C or PASCAL programming languages, this means that the function works with local variables and this is easily implemented by Mathematica using initialized local variables within a Module[]
for example
myFunc[x0_,y0_]:=
Module[{x=x0,y=y0},
.
.
.
]

The local variables x and y have the values of the parameters of the function parameters x0 and y0, respectively.

For a call by reference, the issue is (a little bit) more tricky, since most of the new users do not get to function attributes at all. So for the ones that do not know what function attributes are, please refere to the Mathematica help

SetAttributes[myFunc, HoldAll];
f[x0_Symbol]:=
Module[{declare your local variables here},
.
.
.
x0 = some value (* this is the
assignment to the
global variable x0 *)
]
The HoldAll attribute prevents Mathematica from evaluating the parameter x0 before running the code of the function, and the unchanged argument is inserted to the body of the function.

To the ones that really cares about this issues, it is recommended to read also on HoldFirst, HoldRest etc.

I hope this clarifies this issue
yehuda

URL: ,
Help | Reply To Topic