Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

what is a "return a value"

  Asked By: Jason    Date: Jul 17    Category: Java    Views: 884
  

i am very much new to this world of java, hope some one will help me to
understand some basics, i just want to know what is "return value of a
function?"

is it the out put of a function?, if a function has more than one output which
one is considered to be a return value ? i am totally confused about this simple
concept, plz help me, let me know, am i learning the concept in wrong way?.

hope some one will care to help me with patience to explain me this

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Adal Fischer     Answered On: Jul 17

Here is the modified version of the classic HelloWorld
program.

class HelloWorld { // line 1
public static void main(String[] args) { // line 2
System.out.println("Hello World!"); // line 3
- Output to console.
String returnValue = getMySite(); // line 4
System.out.println(returnValue); // line 5 Output the
return value
}
public static String getMySite(){ // line 7 -
define function
return "http://javarss.com"; // line 8 - function
return value.
}
}

On line 4, main method calls getMySite function. On
line 8, getMySite returns a value. On line 5, the
returnValue is printed to console (screen).

 
Answer #2    Answered By: Devlan Jones     Answered On: Jul 17

A little more information perhaps ...

There are two types of return  statements. Some return a value, others don't.
In both cases, when you get to a return statement, that will be the last thing
executed in the function  - i.e. the function sill stop at that point and the
processing will go back to the caller of the function.

This is useful to end the processing of a function somewhere other than at the
bottom of it. In particular, returns are used to terminate a function when a
required situation is identified inside some sort of search - e.g. you're
buzzing through an array looking for the item you're interested in. When you
find it, you want to do something, then the function can stop. So find it ("if"
statement), do the work and stop the execution ("return" statement).

Returning a value is an optional part of a return statement and is required in
those functions that are expected to return a value. Dimaz said that a function
can return only one value. This needs clarification. A function can have as
many return statements as you like, and each can return a different value. But
remember that the function will stop execution when it encounters a return, so
only one will be executed on any call. Additionally, the thing returned by
"return" is a single item - could be an integer or a string - but it could also
be a complete (and complex) object. So, although it's one item, it might
contain multiple things.

As someone with a mathematical bent, I tend to use the old trigonometrical
functions as examples. Consider:

hypotenuse = opposite * sine (angle)

The sine looks like a function call, and is. For some unknown reason, the
function that calculates sine is always abbreviated to "sin" and the angle is
provided in radians - but these are minor technicalities. Consider:

double function sin (double angleInRadians)
{
magic stuff
return sineOfAngle;
}

The "double" in the function description tells us that we're going to be
returning a value and that that value will be a double by the time it gets back
to the caller.

This means that there must be at least one "return" statement in the function,
returning a value which is capable of being converted to a double for return to
the caller. If needed, there can be many "return" statements, but they must all
return doubles or items capable of being converted to doubles. There must not
be any "return" statements which don't give a value to return, and there must be
a "return" statement for every path through the code in the function.

It'll all become clear once you write your first function that needs to return a
value.

 
Answer #3    Answered By: Heru Chalthoum     Answered On: Jul 17

The return  value of a function  is pretty much the main result of that function.
The main reason why that function exists.
One function only can return 1 value. That's all I know.
Hope it helps.

 
Answer #4    Answered By: Murad Bashara     Answered On: Jul 17

To return  more than one value from the method

1. use Exception ( method will treturn either excetion or return value )

public Object callMethod throws UserException

2. use composite object to return more than one Object


public ReturnObject callObject
{

ReturnObject ro = new ReturnObject();

ro.setResult( // vec , // hashtable ..... etc ) . // if the setResult
accepts Object as arguement

return ro ;

}

 
Didn't find what you were looking for? Find more on what is a "return a value" Or get search suggestion and latest updates.




Tagged: