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 › newb Question about functions return value
Page Index Toggle Pages: 1
newb Question about functions return value (Read 295 times)
newb Question about functions return value
Dec 30th, 2008, 3:26pm
 
Hello!

This is a fairly simple Question about a
functions return value.

How can I return two variables / values?

Example:

int Do_A_Test (int a, int b)
{
   a= 4;
   b= 5;  
   return a;
}

Doesn't work for b.

I dont want to use global variables and I dont't want my function to return an array (but I know that would be one solution).

Can I pass a variable to a function by value and by reference? How to determine which is used?

Thanks!

Greetings,

Chris





Re: newb Question about functions return value
Reply #1 - Dec 30th, 2008, 4:16pm
 
hi !

you can only return a single object in java.
however you can return a compound object.

in your case :

class Couple{
  public int a;
  public int b;
}

Couple Do_A_Test (int a, int b)
{  
  Couple cpl = new Couple();

   cpl.a= 4;
   cpl.b= 5;    
   return cpl;  
}

OR

Couple Do_A_Test (Couple cpl)
{  
  Couple cpl = new Couple();

   cpl.a= 4;
   cpl.b= 5;    
   return cpl;  
}


in Java ALL variables are references. the only exception are the basic types : byte, int, char, float, double long etc.. but not String. Basically all types that are objects have their type(or class) name starting with upper case. Long,Double,String, etc are objects (accessed by reference then...) BUT double, float, int etc are not and are passed by value.

for instance:

String str1="abc";
String str2="123";

str2=str2;//java affects by reference only !
str1="blahblah";

System.out.println(str2); //this will print "blahblah" and not "abc" !


cheers !


Re: newb Question about functions return value
Reply #2 - Dec 30th, 2008, 6:26pm
 
Thank you so much for the quick reply!

Have a very good new year!

Greetings,

Chrisir
Page Index Toggle Pages: 1