Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Meet Objects Privacy and Methods

Confused about return value, where are we returning the value?

Why am I returning a value and to what am I returning it? I don't see any connection

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Al-Noor;

Welcome to Treehouse!

I am a bit confused by the question, but if I am understanding things correctly you aren't understanding the reasoning behind a function returning a value of some sort, correct?

Basically the return is the answer you are wanting from the function, as it is defined.

Let's say that we have a function that we are having do some mathematical computation on two numbers, num1 and num2. Let's make this simple and say that we are going to call the function addTwoNumbers. At the end of our function definition we return the answer so that it can be used elsewhere without always having to write the code inside our function. This is a little silly for this example as our code would look similar to:

public int addTwoNumbers( int num1, int num2) {
  int sumOfTwoNumbers = num1 + num2;

  return sumOfTwoNumbers;
}

Probably not worth the trouble for this example, but say that inside that function you were doing a database query and at the end of the function you want back (returned) some information. The return type doesn't have to be a number, it can be a string, an array, whatever. You just need to declare the return type when you create the function. In the above example that is what the int is for in the function declaration. It let's Java know that the function will be returning an int. You will get error messages if you declare that an int is the return type and then attempt to return something else.

Does that help at all?

Ken

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Someone will call your method and receive your returned value.

Still confused, or did it click?

basically and simply the return function is to get value from variable or method and put it to another variable or method ,, example public boolean ex (int num) { if( num == 1) return true; } if someone call or invoke this method (ex) will return true if num equals 1 if not the method will return false .

the simplest way : ex: if ken doesn't know how to counting his money and then, he asks (in java calling or invoke) craig to count his money then, craig after sums money he returns the total of money to ken .

hehehehehe i hope you understand what i'm saying ...

all best ...

Ben Morse
Ben Morse
6,068 Points

Say your boss is named Main and he is calling his accountant, The function or class, to get him the reports for this months. The accountant will disappear and "return" with the information for Mr.Main.

public class accountMan { //The accountant will go to the location where the information is Privately stored. private int janReport = 100000;
private String mInformation = ("Here is this month's report: " + janReport);

public String getAccountReport { //Once he has the information he will "return" it back to his boss who "call" for the report return mInformation; }

}