Methods
Methods
1
What's a Method?
method = procedure, function, subroutine,
subprogram
tool for dividing your program into pieces
avoid repeating yourself
makes program simpler to read & understand
Think of a method as a small piece of your program
that
knows how to do one specific task
2
Simplest Example
method definition:
/**
* Writes a greeting to the screen.
*/
static void writeGreeting() {
System.out.println("Welcome to NVU !");
} // end writeGreeting
method use:
writeGreeting();
technical detail: put static before declarations
for methods in main class
void means no return value. Method call is a
statement.
3
Methods with Parameters
sometimes a method needs information to
accomplish its task
Example:
compute course mark
information needed: assignment, quiz & final exam
marks
4
Simple Example
5
Method With Parameters &
Result
/**
* Computes the average of two doubles.
*
* @param x the first of the two numbers to average
* @param y the second of the two numbers to average
* @return the average of x and y
*/
static double average(double x, double y) {
double sum = x + y;
double result = sum / 2;
return result;
} // end average
to call:
double avg = average(1.1, 3.4);
6
Advantages of Using
Methods
average is a tiny method, hardly worth the trouble
imagine a longer computation
• separates code into pieces, easier to read
• if you had to do this computation more than once,
put
it in a method & you only write it once: program is
shorter, easier to change
7
Caution About Global
Variables
Bad style:
public class Globals {
static double a, b; // global variables
static double average() {
return (a+b)/2;
} // end average
static void main(String args[]) {
a = ...;
b = ...;
... average();
} // end main
} // end class Globals
8
Better Style With
Parameters
Use parameters instead:
public class UseParameters {
public static double average(double a, double
b) {
return (a+b)/2;
} // end average
public static void main(String args[]) {
double a, b; // local variables
a = ...;
b = ...;
... average(a, b);
} // end main
} // end class UseParameters
9
Parameter Semantics
public class parameters {
static void intMethod(int i) {
i = i + 1;
System.out.println(i);
} // end intMethod
public static void main(String args[]) {
int myInt = 5;
intMethod(myInt);
System.out.println(myInt);
} // end main
} // end parameters
Parameters of primitive types passed by copy: "call by value“
methods do not change the value of their parameters
Output:
6
5
10
Local Variables
declared inside a method
temporary workspace
int computeSum(int N) {
int sum = 0;
for (int i = 1; i <= N; i++) {
sum = sum + i;
} // end for
return sum;
} // end computeSum
lifetime:
• created during call to computeSum
• destroyed when computeSum is finished
11
Lifetimes
12
Scope (1)
13
Scope (2)
14
Scope (3)
15
Scope (4)
16
Scope (5)
17
Scope (6)
18
Scope: Moral
Declare most local variables at top level of
method, you can declare short-term locals
inside inner block
Be careful about name conflicts
19
Overloading Methods
int sum(int a, int b) {
return a+b;
}
int sum(int a, int b, int c) {
return a+b+c;
}
int w, x, y, z;
z = sum(x, y); // uses first version
w = sum(x, y, z); // uses second version
double sum(double a, double b) {
return a+b;
}
double d = sum(3.2, 4.5); // uses third version
int x;
double d, e // uses third version
e = sum(d, x);// converts x to double first
20
Overloading (2)