function call by ref versus call by value

edited January 2014 in Questions about Code

Hello all,

I have a question for function call by ref versus call by value

I understand that a function call in processing / JAVA sees a parameter as

  • a call by ref (adress of the value) when parameters is object and
  • a call by value when for int, char, boolean etc.

This alone I find silly, because it behaves differently here... but never mind.

Now I need a call by ref for a boolean (in a boolean parameter for a function)

to do so, I invented a class BooleanClass with only one property

boolean value;

so it works.

But isn't that a solution that is too complicate?

Btw: I don't want to have the boolean as a return type because it already has one.

Greetings, Chrisir

my class:

// ===============================================
class BooleanClass {
  // minor class to allow for call by reference for a function. 
  // The class capsules boolean to make a parameter of a function changeable by the called function
  // (so that it is permanently changed also in the calling function after the called function has 
  // been called) 
  boolean value;
}
// ========================================

Answers

  • edited January 2014

    Java already comes bundled w/ 8 wrapper classes for the corresponding 8 primitive types:

    • boolean -> Boolean
    • byte -> Byte
    • short -> Short
    • char -> Character
    • int -> Integer
    • long -> Long
    • float -> Float
    • double -> Double
  • edited January 2014

    I have a question for function call by ref versus call by value
    This alone I find silly, because it behaves differently here... but never mind.

    Actually, Java only passes arguments as a copy of their values! That behavior is always consistent in all cases! 8-}

    In most programming languages AFAIK, variables can only hold 1 value at a time.
    And each variable is merely a label for the 1st memory address which contains the stored value w/ as many bytes as necessary!

    In Java, variables can be either primitive or reference/pointer/object.
    The former interprets the value as a concrete number value. While the latter sees it as a physical address value! :-B
    The key here is that both of them are values nonetheless! And that's what is passed to functions and assigned to other variables!

    Informally speaking we might say that when a non-primitive variable is passed, that is a pass-by-value case.
    But technically speaking, that's wrong!!! :-O It's still a copy of the value stored in a variable being passed!

    True pass-by-ref is when the memory address of a variable is passed instead of what's stored in it! o->
    System-oriented languages like C, C++ and D allows that. But neither Java nor JavaScript and many others! :(
    Those former types of language have an operator which gets the memory address of a variable.

    If pass-by-ref was possible in Java, we'd be able to modify the value stored in a passed variable argument.
    See my failed attempts below: >:)


    //forum.processing.org/two/discussion/2670/function-call-by-ref-versus-call-by-value
    
    void setup() {
      int a = 5;
    
      makeIt10(a); // expects to make a = 10.
      println(a); // but it fails b/c only a copy of what's stored in a was passed.
    
      exit();
    }
    
    void makeIt10(int a) {
      // attempts to assign 10 to passed variable argument:
      a = 10;
    }
    

    //forum.processing.org/two/discussion/2670/function-call-by-ref-versus-call-by-value
    
    void setup() {
      PVector p = new PVector(10, -20);
    
      makeItAll10(p); // expects to assign p a completely new PVector instance.
      println(p); // but it fails b/c only a copy of what's stored in p was passed.
    
      makeItNull(p); // one more time!
      println(p); // still untouched!
    
      exit();
    }
    
    void makeItAll10(PVector p) {
      // attempts to assign a new PVector object to passed variable argument:
      p = new PVector(10, 10, 10);
    }
    
    void makeItNull(PVector p) {
      // This time, tries to erase passed variable and fails again:
      p = null;
    }
    

  • Thank you!

  • edited January 2014

    When you e.g. program in Word (VBA) you can say ByVal or ByRef (default)

    • ByVal you receive the value in the function (a copy of the var) and can change it: It doesn't affect the value that it has in the calling function
    • ByRef (default) since you get only the reference to the value when you change something in the called function, also the value of the var in the calling func gets changed
  • edited February 2014

    this clarifies what I meant.

    I needed a func that changes a given parameter.

    This does it - but it's cumbersome...

    It works via an extra class...

    (the first approach with "a" doesn't work, either with boolean or Boolean)

    //forum.processing.org/two/discussion/2670/function-call-by-ref-versus-call-by-value
    
    void setup() {
      Boolean a = false;
      println(a); 
      makeItTrue(a); // expects to make a = true
      println(a); // but it fails b/c only a copy of what's stored in a was passed.
    
      // -----------------------------------------------------------
      println("---------------");
    
      BooleanClass b = new BooleanClass(); 
    
      b.value = false; 
    
      println(b.value); 
      makeItTrueBooleanClass(b); // expects to make b = true
      println(b.value);                //  WORKS
    
      // quit 
      exit();
    }
    
    void makeItTrue(Boolean a) {
      // attempts to assign true to passed variable argument:
      a = true;
    }
    
    
    void makeItTrueBooleanClass(BooleanClass a) {
      // attempts to assign true to passed variable argument:
      a.value = true;
    }
    
    // ====================================================
    
    class BooleanClass {
      boolean value;
    }
    // ------------------------
    
  • edited February 2014 Answer ✓
      BooleanClass b = new BooleanClass();
    
      b.value = false;
    
      println(b.value);
      makeItTrueBooleanClass(b); // expects to make b = true
      println(b.value);                //  WORKS
    

    This "cumbersome" example isn't really working!
    It was supposed to change the actual value of variable b, which is a reference to a BooleanClass object.
    After the call, variable b still points to the very same object before, proving that b itself was unaffected! 8-}
    What was actually changed was the variable value, which is the internal field of the object's class!

    So we can change the internals of an object's reference within a function, but we can't assign another value to a passed variable!

    Oh, those 8 wrapper classes like Boolean, Character, Integer, etc., are as immutable as a String is! @-)
    When we "change" their value, another object of it is instantiated, and this newest reference is assigned to the variable! 3:-O
    That's why even a Boolean variable argument can't be changed from a function, b/c it's immutable after all! >:)

  • edited February 2014

    The simple rule is that in Java ALL parameters are passed by value. So any changes made to the parameter inside the method are not available when the method ends.

    So

    PVector v = new PVector(1, 1);
    
    void setup() {
      doubleVectorA(v); 
      println(v); // No change still 1,1
      doubleVectorB(v); 
      println(v); // Now 2,2
    }
    
    // Attempt to change object reference (fails)
    void doubleVectorA(PVector vec) {
      vec = new PVector(2*vec.x, 2*vec.y);
    }
    
    // Attempt to change vector attributes (works)
    void doubleVectorB(PVector vec) {
      vec.mult(2);
    }
    
Sign In or Register to comment.