Passing variables with void methods

Thread Starter

krow

Joined May 25, 2010
49
Hello,

Not sure if this is either too easy or just impossible. Here's the story: I'm processing data from a sensor and I have several methods that do a specific task with those input values.

The actual code is a bit long so there's no need to have you read it all. I wrote something simpler with the basic idea of what I'm trying to do. I'll try to explain it first:

Let's say I have 5 void methods: A, B, C, D and main ("main" is not really the problem here though).

1. Method "A" passes a variable (int data) to method "B"
2. Method "B" does something with that variable.
3. Method "A" also passes the variable "data" to method "C".
4. Now, method "B" and method "C" want to pass the processed data (two variables = one variable from each method) to one more method called "D", which will give me a final result.

So basically, method "D" is taking a variable from both method "B" and "C". I've been trying to do that but I get an error in method "B" and "C" saying that method "D" requires two variables, not one. It's obvious, I'm giving it only one variable per method but method "D" needs two arguments. I hope you're still reading this and sorry if I made it complicated, please check the code below, very simple:

Rich (BB code):
package passing;

public class Passing {

    Passing p;

    public static void main(String[] args) {
        new Passing().sum();
    }

    public void sum() {

        int x = 3;
        int y = 7;
        int mySum1 = x + y;
        int mySum2 = 2 * x + y;

        mult(mySum1, mySum2);
        add5(mySum1, mySum2);
    }

    public void mult(int pass1, int pass2) {

        int myMult1 = pass1 * pass2;
        int myMult2 = myMult1 * myMult1;
        total(myMult1, myMult2);

    }

    public void add5(int a, int b) {
        
        int add = a + b + 5;
        total(add);
        
    }

    public void total(int r1, int r2, int r3) {
        System.out.println(r1+ "\t"+ r2);
    }
}
Any help on this will be much appreciated, thanks.
 

spinnaker

Joined Oct 29, 2009
7,830
You are making this way more complicated than it needs to be. Refer to your functions just as they are written and post the exact error with the offending line marked in some way.
 
Last edited:

Thread Starter

krow

Joined May 25, 2010
49
OK, sorry about that.

Look at the mult() and add5() methods:

The question is, how do I pass variables "myMult1", "myMult2" and "add" to the "total()" method?

I made a mistake in the total() method, I actually wanted to print r1, r2 and r3 but I printed only two :/


System.out.println(r1+ "\t"+ r2 + "\t3);

I hope it's clear now!
 

spinnaker

Joined Oct 29, 2009
7,830
Remove , int r3 from the total method declaration. It is not being used.

Is this supposed to be c++? If so there is no reason no reason to precede each function with public.

You can simply do as follows

public:

Then list all of your public functions.
 

Thread Starter

krow

Joined May 25, 2010
49
No no :( I can't remove r3 because I'm using that argument in the total() method, or at least that's what I want to do, I made it way too complicated. I'll change the two methods that are giving me trouble:

public void mult(int pass1, int pass2) {

int myMult1 = pass1 * pass2;
int myMult2 = myMult1 * myMult1;

//=============================================
total(myMult1, myMult2); //pass variables to 'total()'
//=============================================

}

public void add5(int a, int b) {

int add = a + b + 5;
//=============================================
total(add); // Pass variable to 'total()'
//=============================================

}

public void total(int r1, int r2, int r3) {

int result = r1 + r2 + r3;

System.out.println(result);
}

So is there any way one method can receive arguments from TWO different methods?

I'm using Java by the way. Thanks for your patience.
 

vpoko

Joined Jan 5, 2012
267
The way your total() method is written, you must pass it exactly three arguments. If you want a version that handles two arguments, you need to declare it with a method signature that takes two arguments (with same name, this is allowed because of method overloading).

The other option is to pass a 0 as the third argument. That will work in this case since adding 0 to a number doesn't change it, but be careful because a 0 might change the result in other contexts (like a multiply method).

Bigger picture, I'm confused by what you're trying to do. In the method add5(int, int), you're adding some numbers, getting a single result, and saving it to the variable 'add'. Why are you then passing that result to total()? There's nothing for total() to total since you're only passing it one variable. I'm guessing what you're trying to do is display the result, but there's no reason to co-mingle the summation that total() is doing with the printing. It's good programming practice to separate functions that calculate a value from those that handle UI - total() should be declared with an int return type and should return its result. A separate method can then print that result.
 

Thread Starter

krow

Joined May 25, 2010
49
Thanks vpoko,

Sorry for the mess, I myself got confused by the way I explained everything, I obviously understand what I'm trying to do because I know my project but simplifying the whole story made it too hard.

Well, I have two sensors. I get raw data from the sensors and I must derive some important quantities from them such as voltage, angles in degrees, etc.

I want to use several methods that will do each task separately. e.g 'A()' will calculate voltage, 'B()' will calculate angles, and so on.

I'm using a third method 'C()' that stores all the data coming from A() and B() and that information is being stored in an arraylist, in 'C()'.

For simplicity I'm using only three methods to explain what the problem is about, A(), B() and C(). But in my program, I have many more methods for "derivation" purposes, not only A() and B() like in the example above. So in my program there's one method whose only function is to get all the data, store it in an array and save the information in a file. Having said that, my original question was, is that possible? is it possible for a method to receive arguments from many different methods?

I hope it's a bit clearer now, thank you for your help, you guys gave me a few ideas.
 

Thread Starter

krow

Joined May 25, 2010
49
Because I could normally use return methods for this purpose but the problem is that some of the methods return more than one variable, unless there's a way for a return method to return multiple variables?
 

vpoko

Joined Jan 5, 2012
267
Because I could normally use return methods for this purpose but the problem is that some of the methods return more than one variable, unless there's a way for a return method to return multiple variables?
Yes, you have to put the multiple return values into some kind of collection.

For example, if you want to return two ints, you can create a class with the two ints as members:
Rich (BB code):
class myVars
{
public int a;
public int b;
}
Then, in your total() method, create an instance of myVars and save your local variables to its members:
Rich (BB code):
myVars rvals = new myVars();
rvals.a = a;
rvals.b = b;
You can then return rvals (your total() method should be defined with myVars as the return type).
 
Top