Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jason Perry   on Jul 17 In Java Category.

  
Question Answered By: Devlan Jones   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.

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
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: