Understanding the return


#1

Hi guys!

I’m just playing around processing to learn and understand it.
But I stumbled upon a really basic problem: Where does the return function return to?

After the function has done their job it should return the value, because I wanna save it to calculate an average, or whatever.

int var;

void setup() {
}

void draw() {
  for (int i=1; i<=10; i++) {
    tues(i);
    if ( i>=10) {
      noLoop();
    }
  }

}
int tues(int a) {
  int quad = a*a;
  println(a + "² = " + quad);
  return quad;
}

AFAIK the value of quad gets returned to where the function was called (so I guess it must be inside the loop, right?). But when I try to get the value of quad it always says that there is no variable available.

So where does the value gets returned to?

Thanks in advance =)
Eric


#2

Hi losferatos, you must save the returned tues(i) value in a variable.
you have correctly instantiated a variable var but did not use it.

int var;

void setup() {
}

void draw() {
  for (int i=1; i<=10; i++) {
    var = tues(i); // tues return a value and is assigned in a variable var
    println("variable 'var' = " + var);
    if ( i>=10) {
      noLoop();
    }
  }

}
int tues(int a) {
  int quad = a*a;
  println(a + "² = " + quad);
  return quad;
}

was this what you intended to do?