Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29385
| From | "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| References | <lr46d1$ub3$1@dont-email.me> <functions-20140728032535@ram.dialup.fu-berlin.de> <lr4afk$ngv$1@dont-email.me> |
| Subject | Re: Help understanding methods |
| Date | 2014-07-28 12:07 +0100 |
| Message-ID | <K4OdnRfV5LuYskvOnZ2dnUVZ7sidnZ2d@bt.com> (permalink) |
Scott Dunning wrote:
> One more quick question. My function call is...
>
> int maxValue = max3(a,b,c);
>
> right?
>
> I kinda just added maxValue because nothing was working and I seen a few
> methods being called with an entirely different (variable I guess).
> However, I just reran it without the maxValue part and it ran just fine.
> Why would you want to create a new what I'm assuming is a variable to
> call your function?
There's no /technical/ reason why you need to assign the value returned from
the function (method) to a variable. You can just call:
max3(a,b,c);
if you want. All that does is executes the function and then discards the
value returned from the function. Probably you won't want to do that very
often, though, because in real code -- i.e. code you are running for some
real-world reason (even if it's just curiosity) -- the returned value will mean
something and you'll want to do something with that value (print it out
perhaps, or save as a high score or something).
Normally if a function (method) returns a value, it will be telling you
something interesting or important (that's true in this case too -- without
looking at the value it returns you can't tell whether it's working). If the
function doesn't have anything interesting or worthwhile to tell you about what
it did then there's a way to declare the function as "returning void" -- not
returning anything at all. I won't go into that more because I don't know
whether you've covered that yet.
Also, as someone else mentioned, if all you are going to do is pass the
returned value to another function you can do that "in line", although it's
often a good idea to assign the value to a temporary variable (which has a
descriptive name) so that you/other people can see what's going on more easily.
However, just as an example, here's a method (function) that computes the
maximum of 9 ints using the max3() function you have already defined.
public static int maxOf9(int a1, int a2, int as3, [...snipped...] int a9)
{
return max3(
max3(a1, a2, a3),
max3(a4, a5, a6),
max3(a7, a8, a9));
}
which invokes max3() a total of four times, but uses no explicit temporary
variables[*]. It could as easily be written:
public static int maxOf9(int a1, int a2, int as3, [...snipped...] int a9)
{
int maxOfFirst3 = max3(a1, a2, a3),
int maxOfSecond3 = max3(a4, a5, a6),
int maxOfThird3 = max3(a7, a8, a9));
int overalMax = max3(maxOfFirst3, maxOfSecond3, maxOfThird3);
return overalMax;
}
I leave it to you to decide which you think is clearer. (Personally I prefer
the first in this case, but normally would prefer something more like the
second in more complex -- real-world -- code).
Of course, if you want to use the returned value more than once, then you will
have to save it to a variable. You can only use the "inline" style if you only
want to make one, and immediate, use of that value.
-- chris
[*] at the risk of confusing you: there still are temporary variables to hold
the results of the first 3 calls to max3() but the Java compiler looks after
all that for you, and you don't have to declare anything explicitly -- it's all
done "by magic" for you.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Help understanding methods Scott Dunning <scottwd01@gmail.com> - 2014-07-27 17:46 -0700
Re: Help understanding methods Knute Johnson <eternal@knutejohnson.com> - 2014-07-27 18:13 -0700
Re: Help understanding methods Scott Dunning <scottwd01@gmail.com> - 2014-07-27 18:19 -0700
Re: Help understanding methods Knute Johnson <eternal@knutejohnson.com> - 2014-07-27 18:49 -0700
Re: Help understanding methods Knute Johnson <eternal@knutejohnson.com> - 2014-07-27 21:36 -0700
Re: Help understanding methods Martin Gregorie <martin@address-in-sig.invalid> - 2014-07-28 01:18 +0000
Re: Help understanding methods Scott Dunning <scottwd01@gmail.com> - 2014-07-27 18:56 -0700
Re: Help understanding methods "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-07-28 12:07 +0100
Re: Help understanding methods "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2014-07-28 12:44 +0100
Re: Help understanding methods Roedy Green <see_website@mindprod.com.invalid> - 2014-07-28 07:20 -0700
Re: Help understanding methods Josip Almasi <joe@vrspace.org> - 2014-07-28 21:29 +0200
csiph-web