I've been wondering this for a long time now. Maybe I have some kind of problem, but I just can't grasp the concept of the "return" statement; not just in Java, but in ANY programming language. O_o What I want to know is, if you return something like a variable, to where are you returning it, what happens to the variable, and why do you need to return it? (I don't know how much Java uses the "return" statement, but I've seen it used before. And I know some of you are C/C++ developers, so even if you offer your input on the C/C++ return statement, that would be just fine, too. ) Thanks!

Oh, thank you, but I've already gotten started in programming. Actually, I'm quite far into Java. I just need to know those things about return statements, they just confuse me. But either way, I'll check out that tutorial. Thank you!

When you return from a method, control is passed back to wherever the method was called from (i.e. another method). The return value of the method is inserted into the expression that called the method. For example, in this code

1

intx = 1;

there is no method call, and the value 1 is assigned to the variable x.

The expression is evaluated by passing control to the add() method, executing the code inside the add() method and inserting the result returned from it back into the expression. So after the return from the method, the expression is equal to:

Note here that the type of the answer (int) is of the same type of the variable that stores the answer (sum).

Quote

Do you have to return every variable once you're done with it? (In the method?)

If you ask a question about something to someone, you don't need to know *how* he got the answer, you just need the answer. So there can be only one return statement because your question has only one answer, so there's no need to return anything else.

Quote

to where are you returning it, what happens to the variable, and why do you need to return it?

You return it to wherever you called the function; in the above example you return it the variable 'sum'.The variable you're returning only exists within the function (the code block between { and }), so you need to return it because you want to get the result from the function. The return statement 'copies' the variable to the expression calling the function:In the above example, the variable 'answer' doesn't exist outside of the function 'tellMeTheSumOf', so you need the 'return' statement to 'copy' the answer into the variable 'sum'.

Oh, I get it now. Okay, I'm not that far into Java, but it's not like I'm just starting out. I know about loops, conditional statements, classes, objects, methods, etc. I've just always had trouble contemplating a return statement, even when I did some stuff with C/C++. Maybe it's like some kind of learning disability (or maybe just a brain fart ) but it doesn't matter, it makes sense now. Thanks!

further the Java VM clears the variable only when they're no more linked to a reference, a.k.a the variable name itself. Whatever you want to get from the value you return from some method, Java will clear it unless you keep a so called "strong reference" to it. java code be so with EVERY single instance :

/*** the main will check for the variable after the first unstored call and will look again after the second stored call. You will observe null PointerException */publicstaticvoidmain(String[] args) {try{ getBoolValue(1, 2, "3");if(returnValue)System.out.println("returnValue is " + returnValue); } catch(Exceptione) { e.printStackrace(); } finally { returnValue = getBoolValue(1, 2, "3");if(returnValue)System.out.println("returnValue is " + returnValue); }}}

Of course some readers here would say this is a stupid example, but this is a way to get things explained for such terrific questions....

Actually I think this example is more confusing than helping here, since static variables are something you really need only in a few special cases and learning them to soon misleads beginners to code in java like in C (having one file with a main function and only static variables and methods).

Well think of the simplest applications of return. You have a method or function and you usually want it do to something for you.If you have a LinkedList and you want to have the 3rd object in the list, you call list.get(3), and the get function will return the object. Or you want to know the position of your object, you call list.indexOf(obj) and it will return the position. It will do something like- start at the beginning- if the current object is the searched one, RETURN the current position- else go to the next object until the end of the list is reached

If you call list.size() it will return the number of elements in the list, for example by counting it.

Lets move on, if you need random numbers, you can use Random rand = new Random() and then you need a random Integer between 0 and 100, you will call rand.nextInt(100) and it will return a random int between 0 and 100.

Or the Math class. It does simple calculations for you.Math.sqrt(4) will return the square root of the given number. Math.max(a,b) will return the bigger of the two integers.

Generally, a function can return something or nothing, then its a void function. And a function can take parameters or take no parameters. It depends on what you want it to do.

1) Add something to the list will need a parameter , the object you want to add. But you dont need a return because adding will not return any result.2) Sorting the list does not need a parameter and does not return anything, it just modifies the order of the elements.3) Getting the size of the list will not need a parameter, but will return the count of elements.4) Getting the position of an element will requiere the element as parameter and will return the position.

So a method usually shall do a piece of work for you. E.g. System.out.println("abc") shall make an output in the console. Sometimes you must give the function something it needs to do its work, then you need parameters. Sometimes, the function will produce some kind of result, then you need to return it. And sometimes you need none or both.

I hope this helps. Consider a function as an question or order and return is a response or result or product. If i ask you, how much money do you have with you, you will look in your pocket, count the coins and bills and reply (return) the amount of money you have.

Another example. If you have a sentence and want to get every single word, you can use a StringTokenizer. Calling hasMoreTokens() will return true or false, depending on if there are still words left to process. Calling nextToken() will return the next word in the sentence. So this function does a piece of work for you and by return it gives you a product or result of this work.

Your real question is: How do functions work and what do I need them for? Once you understand that a function does a piece of work you will instantly come to the problem, that you will often need it to give you the result in some way, and there you are. If you need the sine of an angle, you dont compute it yourself, youll call a function which does the math and it will return the value.

The next chapter will be object orientet programming (OOP). This will explain how data and functions are bundled together in reasonable way.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org