Wow, I think return is really easy
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.