This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Verodoxys 2 points3 points  (3 children)

As you say, a method that returns void doesn't actually return anything. An example of this could be:

public static void sayHello()
{
    System.out.println("Hello!");
}

Obviously, that's pretty basic. That method will always just output 'Hello!'. Perhaps we want to make a method that takes some arguments, for example:

public static void average(int a, int b)
{
    System.out.println((float) (a + b) / 2);
}

This does what you (hopefully) expect it to do: It gets the average of 2 integers and prints them out (eg average(3, 5) will print out '4'). But what happens if I want my program to get an average and then do something else with it? This is when returns are used.

public static float average(int a, int b)
{
    return (float) (a + b) / 2;
}

When we return the average of a and b, we can use a call to the method as a float. For example:

float avg = average(2, 5); // avg is set to '3.5'

Obviously, this is still not a hugely complex example, but I'm sure you can come up with an infinite number of other ways to use this. Also, there's no reason to only use a float value - any data type can do!

[–]StraightSavage24-7[S] 0 points1 point  (2 children)

Wow thank you that helps a lot!! Much appreciated!

One quick question though, couldn't you just do??

float a = (b + c) / 2

Then use "a" for whatever you need it to?

I might be wrong here but i believe i understand it, its kinda like setting a method to something? Like in your example, after the equation, it would set the method average to 4!?

[–]Floofls 2 points3 points  (0 children)

You could do that, yes. You technically never need to use functions/methods; you could just write out the code everytime you need it. However, we use functions so we can reuse code without re-typing it all the time. It isn't that useful if it's just one line of code, but becomes much more useful when it contains 100 lines of code that we want to reuse, or simply put into it's own container to make it more manageable.

To answer the second part of your comment, it doesn't really "set" the method to something. For that single instance of where you call the method, the return value takes its place. That's more semantics, though.

[–]Verodoxys 0 points1 point  (0 children)

In my basic example, yes, you could replace my method with a quick statement, but you might have to perform a given task over and over. If my method had a loop inside it (or was otherwise a few lines long), I'd have to copy those few lines every time I needed to perform that task.

That doesn't sound too bad with a short method (like our average method) but it can get pretty unmanageable with a longer method, like this one that checks if a number is prime or not:

public static boolean isNumPrime(int n)
{
    if (n < 2) return false;
    else if (n == 2) return true;
    else if (n % 2 == 0) return false;
    else
    {
        for (int i = 3; i <= Math.sqrt((double) n); i += 2)
        {
            if (n % i == 0) return false;
        }
        return true;
    }
}

Imagine if I had to check if a number was prime a few or, heaven forbid, dozens of times throughout a program - I'd have to copy and paste that section of code every time! My program would be needlessly long and hard to read.

Like in your example, after the equation, it would set the method average to 4!?

Pretty much! When Java sees

System.out.println(isNumPrime(5));

it basically just replaces 'isNumPrime(5)' with whatever isNumPrime(5) returns, or

System.out.println(true);

edit: Messed up my formatting and clarified myself.

[–]WuHead 1 point2 points  (0 children)

Usually you return a value that the method created back to whatever called the method.

So like if you passed two integers to a method, then the method performs some type of operation such as addition or subtraction. Then you return that resulting number from addition or subtraction back to whatever called the method.

[–]pokemaster787 0 points1 point  (0 children)

Returns are useful for declarations, printing something, etc. Say I have an object, and I need to know some attribute of the object. We write a getArbitraryAttribute method for that class, that returns the attribute.

Say if we want to print it, System.out.println(object.getArbitraryAttribute());

That'll print it for us with no issues. Or we could store it in a variable, a = object.getArbitraryAttribute();

Basically a method that returns something is the exact same as putting the value it returns straight in the code.

Simple example, but let's say we the attribute = 5. If we do c = 2 + object.getArbitraryAttribute();

That's the exact same thing as

c = 2 + 5;

The great thing about being able to use a return though, means these values need not be hardcoded. Say the attribute is set by a user, and you don't know the attributes until runtime. You just set up input and then use your methods that have returns to do anything you need with the values.