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 › Call by refernce
Page Index Toggle Pages: 1
Call by refernce (Read 965 times)
Call by refernce
Dec 18th, 2009, 3:33am
 
Hi!

I'm just a beginner in Processing. I was wondering if there is a call by reference concept with functions, so a function can reply more than one answer-values. In C it is realized with the concept of pointers, the pointer to a variable v is adressed with &v, the variable behind a pointer p with *p. Is there a similar way in Processing? Or do I have to realize this with a functions, that returns an array, where the different answer-values are stored?

Thanks,

fweth
Re: Call by refernce
Reply #1 - Dec 18th, 2009, 5:34am
 
In java (and thus processing) references to objects are passed by value. Primitive data types (int, float, double) are passed by value. Nothing is passed by reference.

This little piece of code should demonstrate the previous statements.
Quote:
void setup()
{
  {
    TestClass t = new TestClass();
    t.n = 1;    
    println(t.n);
    testReferencePassedByValue(t);
    println(t.n);
  }
  
  {
    TestClass t = new TestClass();
    t.n = 1;    
    println(t.n);
    testCallByReference(t);
    println(t.n);
  }
}

void testReferencePassedByValue(TestClass t)
{
  t.n = 2;
}

void testCallByReference(TestClass t)
{
  t = new TestClass();
  t.n = 2;
}

class TestClass
{
  int n;
}



So to compare with C.
Java: void testReferencePassedByValue(TestClass t)
is a lot like
C: void testReferencePassedByValue(TestClass* t)
and nothing like
C's call by refence: void testCallByReference(TestClass& t)
Re: Call by refernce
Reply #2 - Dec 18th, 2009, 6:43am
 
JR is perfectly right.
To answer your question, the best way to return several values is to create a class (can be just a list of fields, as JR shown) and return an instance of this class.
You can also pass the object as parameter and alter its values.
Re: Call by refernce
Reply #3 - Dec 20th, 2009, 7:42am
 
Thanks a lot!

fweth
Page Index Toggle Pages: 1