...
I am creating a function to add two numbers without using a return value.
But I cannot find a method to pass a reference to an integer. Thanks:
You cannot pass a variable of a primitive type by reference, actually you
can't pass *anything* by reference in Java. Everything is passed by value,
but the value of a "reference type" is a reference to the object...
public static void iadd(int &a, int b, int c){
a = b + c;
}
Why you would want that behavior goes a bit over my head.
Usually when "converting" code from one language to another, one use to look
more to "what am I trying to accomplish?", "are there other and maybe better
ways to do this in the new language?", etc.
Depending on what that method *actually* needs to do in your program,
there's two solutions from the top of my head.
-------------------------------
The first and I guess most obvious, is to skip the requirement that the
method isn't allowed to give a return value.
public static int iadd(int b, int c) {
return b + c;
}
-------------------------------
The second approach is to encapsulate the "return value" in a wrapper of
some kind.
An example:
class IntHolder
{
public int value;
}
....
public static void iadd(IntHolder a, int b, int c) {
a.value = b + c;
}
....
// Bjorn A