Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!81.171.88.16.MISMATCH!hq-usenetpeers!hq-usenetpeers.eweka.nl!news.astraweb.com!border5.a.newsrouter.astraweb.com!border2.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 28 Jul 2014 06:07:49 -0500 From: "Chris Uppal" Newsgroups: comp.lang.java.programmer References: Subject: Re: Help understanding methods Date: Mon, 28 Jul 2014 12:07:54 +0100 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5512 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512 Message-ID: Lines: 77 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-s5C/obn6Q44rOQt9OdxzNoEVN8IGwXgU4jJrj6y6FD/PgXkhCHvdl9Moi3zXG2X/2TOlCUEqPqZA0iJ!WbA/NDuHQGgppmHTTH/NRXAaWd84XPaXbz0+P+42M3v0X4h1EeySARDp1Bw5fveh/udN0RZw+A== X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4623 X-Received-Bytes: 4816 X-Received-Body-CRC: 115036173 Xref: csiph.com comp.lang.java.programmer:29385 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.