0% found this document useful (0 votes)
7 views21 pages

Methods

The document explains the concept of methods in programming, defining them as procedures that help organize code and avoid repetition. It covers method definitions, the use of parameters, advantages of using methods, and the importance of local variables and scope. Additionally, it discusses method overloading and the rules governing it.

Uploaded by

zb8dbvhnq2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

Methods

The document explains the concept of methods in programming, defining them as procedures that help organize code and avoid repetition. It covers method definitions, the use of parameters, advantages of using methods, and the importance of local variables and scope. Additionally, it discusses method overloading and the rules governing it.

Uploaded by

zb8dbvhnq2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

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)

public class scope3 {


public static void main(String args[])
{
if (...) {
float x = 4.2;
System.out.println(x);
} // end if
x = 5.7; // ERROR
} // end main
} // end scope3

Can declare variables inside inner blocks.

They last until the end of the block.

15
Scope (4)

public class scope4 {


public static void main(String args[])
{
int x;
while (...) {
int x; // ERROR
} // end while
} // end main
} // end scope4

No hiding variable declarations within a method

16
Scope (5)

static void myMethod(int x, double y) {


int x; // ERROR
char y; // ERROR
} // end myMethod

local variables can't hide formal parameters
-- same scope

17
Scope (6)

public class scope6 {


public static void main(String args[]) {
if (...) {
int x = 3;
System.out.println(x);
} //end if
while (...) {
String x = "hello";
System.out.println(x);
} // end while
System.out.println(x); // error
} // end main
} // end scope6

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)

int sum(int a, int b) {


return a+b;
}
double sum(int c, int d) {
return (double) (c+d);
}

Not legal -- differ only in return type
must differ by number or type of parameters

Note: parameter names not significant
21

You might also like