120 likes | 334 Views
Introduction to Computing Concepts. Note Set 18. Methods. Using the divide and conquer method of problem solving. Take a big problem Break it down to component pieces Code those pieces separately. Allows the programmer to reuse code rather than re-typing/redeveloping it
E N D
Introduction to Computing Concepts Note Set 18
Methods • Using the divide and conquer method of problem solving. • Take a big problem • Break it down to component pieces • Code those pieces separately. • Allows the programmer to reuse code rather than re-typing/redeveloping it • Think about all of the GUI components you’ve used
Anatomy of a Method private double square (double number) { return number * number; }
Method Header & Body private double square (double number) { return number * number; }
Multiple Parameters private double sum(double number1, double number2) { return number1 + number2; }
No parameters • Void return type – Nothing is returned • Empty () – method doesn’t accept any params. private void printHello() { System.out.println(“Hello”); }
Calling a method • Making the method execute is called invoking the method. double val = square (1.23); Invokes the method and sends 1.23 to the parameter
Flow of Control • After a method is done executing, it returns to the place where it was called. public void doubleAndPrint(int x, int y) { int ret = square(10); System.out.println(ret); ret = square(20); System.out.println(ret); } public void square(intval) { int answer = val * val; return answer; }
Practice • Implement a method that will return the larger of two parameters passed to it.