flutter
flutter

Reputation: 6766

returning a value from a method to another method

Can somebody tell me why the value returned is 3 and not 8. Doesn't the return x statement from the addFive method change the value of x in the main method?

public class App {
    public static void main(String[] args) {
        int x=3;
        addFive(x);
        System.out.println("x = " + x);
    }

    private static int addFive(int x) {
        x += 5;
        return x;
    }
}

Upvotes: 4

Views: 63148

Answers (8)

Evan Bechtol
Evan Bechtol

Reputation: 2865

You have to set the returned value to a variable, otherwise it is lost and you are retrieving the value of "x" in your main method. Do this instead to capture the return value.

   public static void main(String[] args) {
       int x=3;
       x = addFive(x);
       System.out.println("x = " + x);
   }

If you only want to see the returned value and not store it, you can even put the function call inside the System.out.println.

   public static void main(String[] args) {
       int x=3;
       System.out.println("x = " + addFive(x));

   }

Upvotes: 5

Ski
Ski

Reputation: 111

The method does return a value, but you have to set the value to a variable too when you return it, or else how would it know which variable you want to return the value to? You can have 10 variable, and if you just call the method, how will it know which variable to return the number to? That's why you have to set the returning number it to a variable like this:

x = addFive(x);

Upvotes: 1

Joe Doe
Joe Doe

Reputation: 141

when you call a method in java and you don't assign for any variable, the changes will happen in the method call, after that this value will be lost and go back to the value that you assign. to see the result you should do that System.out.println(addFive(x)); but if you want to change the value of x you have to assign x = addFive(x);

Upvotes: 2

Mehmood Arbaz
Mehmood Arbaz

Reputation: 295

The value in the main function is completely different from the value in the addFive(int x ) function. You send an x from main to addFive(int x) method.

JVM makes a copy of x and send it to addFive(int x) method. Then x changes in the addFive(int x) method. But the x in main() method remains unchanged.

If you want to get the changed value returned by addFive(int x) from main method you can do the following -

int returnedValueFromAddFive = addFive(x)  

Hope it will help.
Thanks a lot.

Upvotes: 0

Sidmeister
Sidmeister

Reputation: 856

You are calling the method addFive(int x) with x, but not assigning the returned value to anything. So, inside main()'s scope x remains as before, 3 - which is what is being printed. So, you can either store the returned value to x itself:

x = addFive(x);

or make the function call within print statement:

System.out.println("x = " + addFive(x));

Upvotes: 1

adamclmns
adamclmns

Reputation: 29

Like everyone else is saying, you need to assign your return value. Because you're doing "addFive(x)" instead of "x=addFive(x);" you're just printing the instance of "x" in main, and not ever getting the value that your function returns.

This is because "x" in your main function is an instance variable, and your "x" in addFive() is a local variable. These are not the same variable, even if they have the same name. This might clarify a bit - http://www.tutorialspoint.com/java/java_variable_types.htm

Upvotes: 3

Jaken
Jaken

Reputation: 1343

You want x=addFive(x); rather than just addFive(x). Calling addFive(x) on it's own does not apply the returned value to any variable.

Upvotes: 10

slnowak
slnowak

Reputation: 1919

Because you're simply not using the computation of your function. It doesn't change value of x, it returns new value.

You should do something like:

int y = addFive(x);

Upvotes: 0

Related Questions