return values from functions

19 views
Skip to first unread message

Michael Lomman

unread,
Nov 19, 2018, 1:33:58 AM11/19/18
to Processing.js
Two questions, can a function return both a string array and an int array? If so what would the function call use? ie it couldn't be void, 


//function to populate arrays
//NB whilst the data is hard coded in this example, in real life scenarios the data would be imported from a file or data base.

String[] populateArrays(){
arraySize = 13;  //there are 13 various major govt. sectors
string[] govtSector  = new string[arraySize]; // this will hold the separate govt sectors
int[] budget  = new int[arraySize]; // this will hold their budgets
string[] govtSector = {'General Public Services ', 'Public Order and Safety ', 'Education ', ' Health ', 'Social Security and Welfare ', 
'Housing and Community Amenities ', 'Recreation and Culture ', 'Fuel and Energy ', 'Agriculture, Forestry, Fishing and Hunting ',
'Mining and Mineral Resources other than fuels ', 'Transport and Communications ', 'Other Economic Affairs ', 'Other Purposes '};

int[] budget = {'408','1752','4364','5618','1437','1275','421','74','213','73','1219','390','766'};
return govtSector;
// return budget;
}//end populateArrays

populateArrays();

the second issue is related to the first, as I cannot get either array govtSector[] or budget[] to return from the function.

What should replace String[] in the above function for the arrays to be returned?

Mike

Lee Brunjes

unread,
Nov 19, 2018, 7:47:17 AM11/19/18
to proces...@googlegroups.com
The Java answer here is to use a map https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

Processing.js is JavaScript under the hood so, alternately you can use objects to do the same thing. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

The last option is to return an array of user defined classes, though it is likely over kill for a string int mapping. 

-Lee

--
You received this message because you are subscribed to the Google Groups "Processing.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to processingjs...@googlegroups.com.
To post to this group, send email to proces...@googlegroups.com.
Visit this group at https://groups.google.com/group/processingjs.
For more options, visit https://groups.google.com/d/optout.

Michael Lomman

unread,
Nov 19, 2018, 8:30:41 PM11/19/18
to Processing.js
Thanks Lee, 

Michael Lomman

unread,
Nov 20, 2018, 12:22:18 AM11/20/18
to Processing.js
On further inspection, I didn't declare the array correctly outside the function so it wasn't available to the next function. Maybe there is a way it could be declared inside the first function and still be available to the second, but I don't know it.
I did separate the first function into two separate functions, one dealing with the string[] and the other with the int[]. Yes a hash map could do this.
Also my int[] contained strings not integers.
Reply all
Reply to author
Forward
0 new messages