Return two values from a function

edited June 2015 in Questions about Code
 int[] temp = new int[2];
int a=1,b=10;
void setup()
{

}
void draw()
{

function(a, b); println(temp[0]);

}

int[] function(int a,int b){ 

  temp[0] = a*10; 
  temp[1] = b*20;
  return temp;
} 
Tagged:

Answers

  • edited June 2015

    better say temp = function...... in the line 9

    you must store the return value.

    also: in the func declare temp2 instead of temp and use temp2 there

    atm the func is just changing the global array. bad.

    instead you want it to work with the local array.

    and return this.

    ;-)

  • can you post the edited code. I dint get it completly

  • Answer ✓
    int[] temp = new int[2];
    int a=1,b=10;
    void setup()
    {
     
    }
    void draw()
    {
    temp= function(a, b); 
    
    println(temp[0]);
    
    
    }
     
     int[] function(int a,int b){ 
     int[] temp2 = new int[2];
      temp2[0] = a*10; 
      temp2[1] = b*20;
      return temp2;
    } 
    
  • edited June 2015 Answer ✓

    You don't have to call the function array temp2 simply call it temp the compiler can tell the difference between the 2 variables called temp based on variable scope.

    In fact for something so simple you could use an anonymous array like this.

    int [] function (int a, int b){
      return new int [] {a * 10, b * 10};
    }
    
  • edited June 2015

    function() can be shortened as:

    int[] function(int a, int b) {
      return new int[] { a*10, b*20 };
    }
    

    And no need to initialize variable temp as in here: int[] temp = new int[2];
    B/c temp = function(a, b); re-assigns a new int[] array after all, discarding any previous 1s! :-B

    P.S.: Oops, took so much to refresh the page that I wasn't aware @quark had the same observation! 3:-O

  • you guys rock,this plays has great ppl to support =D>

  • @Chrisir @GoToLoop i remember from my programming classes that my teacher told that avoid changing global variables in a function rather make local ones and then simply copy coz bad things can happen. This is where the private public features of classes comme handy. But pls can you share what bad things might happen and whay is it not a recommended practice.

  • edited June 2015 Answer ✓

    If you have a large program with many functions that share global variables it becomes difficult to debug your program for logical errors.

    For instance assume you have 2 global variables netprice and taxrate you could have a function.

    float tax2pay (){
      return netprice * taxrate / 100;
    }
    

    If we calculate 20% of £50 then it should return £10 but what if it returns something different. It means that some other function has changed either netprice or taxrate between the time they were set by us and the time the function was called. If these variables are shared by many functions in a karge program it would be difficult to find where this happened an so fix it.

    This is called a logical error. A logical error is where the program compiles and executes but does not do what is expected. Logical errors are the hardest ones to find and fix.

  • edited June 2015 Answer ✓

    ... my teacher told that avoid changing global variables in a function...

    • Actually Java doesn't have "global" variables but fields.
    • In the OOP paradigm a method, which is simply a function that belongs to a class, has the "right" to access any fields of the same class it belongs to.
    • Only when dealing w/ "foreign" fields we should pass them as parameters for that method.
    • In the context of your sketch, method function() belongs to the "sketch" class.
    • So it has the rightful "permission" to access sketch's temp field directly if it so "wishes to".
    • If it decides not to, that is, a method doesn't access any of its class' fields directly, rather any of them are passed as its parameters, it is considered a stateless method then.
    • In those cases, it's good to mark them as static, meaning it can be used w/o instantiating its class.

    static final int[] function(int a, int b) {
      return new int[] { a*10, b*20 };
    }
    

    https://processing.org/reference/static.html

  • It is true that with object orientated kanguages we do not talk of global variables these came from procedural languages. In Java we do not have global variables but we do have public static variables which have global scope. In Processing we talk of global variables but in truth these are class instance fields.

    That said what your tutor said is correct and the premise can still be applied to Java programs and Processing sketchs.

Sign In or Register to comment.