how to pass by reference??

Steve Holden sholden at holdenweb.com
Wed Feb 6 13:27:29 EST 2002


"Spencer Doidge" <spencer at spencerdoidge.com> wrote ...
> I really tried to RTM first this time, but I failed.
> Can someone give me a hint where in the doc html's it explains how to pass
> by reference?

The same place it explains how to use arguments :-) -- by reference is the
only way to do it.

References to immutable objects, however, (strings, integers, etc.) cannot
be used to change the value, since the function argument is a *local copy*
of the references passed in. Binding that local copy to a new value will
have no effect on the argument that was passed in to the function.

> Specifically, I want to do the equivalent of this:
>
> char s[10];
>
> void foo(char *bfr)
> {
> bfr[0] = 'a';
> bfr[1] = 'b';
> return;
> }
>
> void main(void)
> {
> foo(s);
> return;
> }
>
Because strings are immutable there is no way to do this. You'd have to
return a new string value form the function and bind it to the name you
passed in as an argument.

regards
 Ste e
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, Python Web Programming: http://pydish.holdenweb.com/pwp/

"This is Python.  We don't care much about theory, except where it
intersects with useful practice."  Aahz Maruch on c.l.py







More information about the Python-list mailing list