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 › Re: Int[] to String ///and///Int[] to String[]
Page Index Toggle Pages: 1
Re: Int[] to String ///and///Int[] to String[] (Read 870 times)
Re: Int[] to String ///and///Int[] to String[]
Dec 9th, 2008, 6:28am
 
A simple function that returns a string should solve your problem:

Code:

String intToStr (int[] intArray){
String output = new String();
for(int i = 0;i<intArray.length();i++){
 output = output+" "+intArray[i];
}
return output;
}


Afaik "Code Unreachable" refers to some part of your code that the compiler identifies as, for some reason or another, code that will never be executed. Something like this for example:

if(x == 1){
...
} else {
if (x == 1){
/*whatever is inside this if bloc will never be executed
because the else bloc only gets called in cases where x is
not 1. */
}
}

Re: Int[] to String ///and///Int[] to String[]
Reply #1 - Dec 9th, 2008, 5:47pm
 
Since 'output' is declared inside of the function it won't be accessible outside the function. That means you can't access output from ' void setup()' instead the function "returns" a String.

Notice how my previews example says 'String intToStr (int[] intArray)' instead of 'void intToStr (int[] intArray)'

A 'void' function executes whatever statements are inside of it and that's it. A 'String' (or int or float or ...) function returns a string.

This example shows a 'void' and an 'int' function:
Code:

void setup(){

randomFunction(); // can be called by itself

int i = randomFunction2(); // needs to be assigned to a variable that is the same as the function. Now your variable 'i' has been assigned whatever your function returned, in this case 'result'

}

void randomFunction(){
//executes some statements
}

int randomFunction2(){
int result; //this variable is declared within this function and thus can't be accessed from outside of it

//executes some statements

return result; //needs a return statement. This tells the function what to pass back
}

Page Index Toggle Pages: 1