Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Help understanding methods Date: Sun, 27 Jul 2014 18:13:32 -0700 Organization: A noiseless patient Spider Lines: 59 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 28 Jul 2014 01:13:31 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="aba33539224e5c782fe0c4053f7756fd"; logging-data="12084"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Abptq/+2W4/CgMFPIxmLH" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: Cancel-Lock: sha1:9fgbI+PDfQe6wddj/H7gHgNHI58= Xref: csiph.com comp.lang.java.programmer:29374 On 7/27/2014 17:46, Scott Dunning wrote: > So, I'm very new to Java, I have a summer college course and we're on > functions (methods) and I'm having a bit of trouble understanding them. > > There is a question on a lab I'm having a little trouble with: > > "Write a method called MaxOfThree that accepts three integer parameters > and returns the largest of the three." > > This is what I have so far but I'm not sure whats wrong. I added the > print statement at the end because I wasn't getting a return value when > I ran it but now I'm getting errors and it's not compiling. If you could > help me understand methods a bit more I'd greatly appreciate it. For > instance how the parameters work and calling it and if what's included > in the function call is correct and how that works. I just get so > confused when I read through the material and was hoping for an > explanation in more layman's terms. Thanks for any help, here is what I > have so far. > > public class Test { > > public static void main(String[] args) { > int a = 2, b = 3, c = 4; > int maxValue = max3(a, b, c); > } > > public static int max3(int a, int b, int c) { > int max = a; > if (b > max) max = b; > if (c > max) max = c; > return max; > System.out.println(max); > } > } > > > Here are the errors I'm receiving just in case... > > Test.java:16: error: unreachable statement > System.out.println(max); > ^ > Test.java:17: error: missing return statement > } > ^ > 2 errors The return statement ends the code path so the System.out line can never be reached. The second error is because there is not a return statement as the last thing in the method. To fix it: move the print statement to the main method That should do it. -- Knute Johnson