We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › passing array of strings to a function
Page Index Toggle Pages: 1
passing array of strings to a function (Read 914 times)
passing array of strings to a function
Aug 25th, 2008, 8:51pm
 
Hi

I have been trying to understand passing an array of strings to a function as a parameter.

In the Processing book it says when you pass an array it just copies the location as a parameter and any manipulation of the array within the function is changing the original contents of the array, and will persist after the function has finished.

This is the behaviour with an array of floats, but if I pass an array of Strings, this doesn't seem to happen and the changes I make to the array are not reflected in the original.

I have to declare the return type of the function as String[], and explicitly return the array from inside the function, and assign it to the array that gets passed in to get them out of the function.

eg thingsToType is a String[], and I am having to use

thingsToType=loadPrefs(thingsToType);

I realise String is a Composite data type rather than a primitive and suspect it is to do with this.

Its working for me, but I am wondering whether this is the best way to do it. Is the function having to copy the contents of the array to return it, is there a better way?

Thanks
Re: passing array of strings to a function
Reply #1 - Aug 25th, 2008, 9:52pm
 
Quote:
In the Processing book it says when you pass an array it just copies the location as a parameter and any manipulation of the array within the function is changing the original contents of the array, and will persist after the function has finished.

It does.

Try :

Code:
void setup() {
String[] array = new String[]{ "a", "b", "c" };
change(array);
println(array);
}

void change(String[] arr) {
arr[0] = "z";
}
Re: passing array of strings to a function
Reply #2 - Aug 25th, 2008, 11:28pm
 
Yep your example does exactly what I would expect.

So in the function I a using I am loading text from a file - in this stripped down test a file called fred.txt, that has a few lines of text. I have stripped out all the code that checks whether the file is there and does different things if it isn't (I thought maybe what it did if the file wasn't there was causing the problem as it was initialising the array with  new statement).

But stripping the code right down to just this still produces the behaviour.


Quote:


String[] outsideArr={
 "a", "a", "a"};

void setup(){
 println(outsideArr);
 loadPrefs(outsideArr);
 println("outside");
 println(outsideArr);

}

void loadPrefs( String[] arr){
 arr= loadStrings("fred.txt");
 println("inside");
 println (arr);
}





Produces

[0] "a"
[1] "a"
[2] "a"
inside
[0] "me"
[1] "you"
[2] "banana"
outside
[0] "a"
[1] "a"
[2] "a"

If I send the array in empty I get

null
inside
[0] "me"
[1] "you"
[2] "banana"
outside
null
Re: passing array of strings to a function
Reply #3 - Aug 25th, 2008, 11:38pm
 
Ah right, there's a tiny bit of confusion going on.

Behind the scenes, here's what's going on.

You're putting "a" "a" "a" into a box somewhere, and writing the location of the box on a piece of paper.

you're then copying the bit of paper, and giving the copy to your function.

in your function you're then erasing what's on the copy of the piece of paper and writing a different location, where there's a box with the contents of fred.txt

Now back in the outside function, that piece of paper still has the location of the original box written on it, so prints "a" "a" "a" again.

In the examples that do change things, what's being changed is not the location written on the piece of paper, but the contents of the box.
Re: passing array of strings to a function
Reply #4 - Aug 25th, 2008, 11:49pm
 
This is one of those very funny things about parameter passing.  Yes, you are passing in a reference to the array in memory so that you can change the contents of that array.  However, you cannot change the reference itself.  And this is what you are doing in your code -- setting the array in its entirety to a new array (as opposed to the individual elements themselves.)  For example, this will work:

Code:

void loadPrefs( String[] arr){
String[] arr2= loadStrings("fred.txt");
for (int i = 0; i < arr.length; i++) {
arr[i] = arr2[i];
}
println("inside");
println (arr);
}


However, what I would suggest doing in this case is simply returning a reference to the new array.

Code:

String[] outsideArr={
"a", "a", "a"};

void setup(){
println(outsideArr);
outsideArr = loadPrefs();
println("outside");
println(outsideArr);
}

String[] loadPrefs(){
return loadStrings("fred.txt");
}
Re: passing array of strings to a function
Reply #5 - Aug 26th, 2008, 12:01am
 
Thanks all.

Thats what I had ended up doing, but I was unsure why it wasn't working the way I expected.

Much clearer now.
Page Index Toggle Pages: 1