Java OOP
Functions
Lec6
Mohammed Sultan 1
Methods
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also
known as functions.
Methods
• 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
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
• mneumonic name makes purpose of computation clearer
2/8/2024 4
Parameters and Arguments
• Information can be passed to methods as parameter. Parameters act
as variables inside the method.
• Parameters are specified after the method name, inside the
parentheses. You can add as many parameters as you want, just
separate them with a comma.
public class Main {
static void myMethod(String fname) {
[Link](fname + "is student");
}
public static void main(String[] args) {
myMethod("Ali");
myMethod("Ahmed");
}
}
Argument vs parameter
• When a parameter is passed to the method, it is called an argument.
So, from the example above: fname is a parameter, while Ali
argument
Return Values
• The void keyword, used in the examples above, indicates that the
method should not return a value. If you want the method to return a
value, you can use a primitive data type (such as int, char, etc.)
instead of void, and use the return keyword inside the method
int myMethod(int x) {
return 5 + x;
}
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
2/8/2024 9
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
2/8/2024 10
Parameter Semantics
public class parameters {
static void intMethod(int i) {
i = i + 1;
[Link](i);
} // end intMethod
public static void main(String args[]) {
int myInt = 5;
intMethod(myInt);
[Link](myInt);
} // end main
} // end parameters
• Parameters of primitive types passed by copy: "call by value“ methods do not change
the value of their parameters
• Warning: object types are a bit different
• Output:
6
5 2/8/2024 11
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
2/8/2024 12
Lifetimes
2/8/2024 13
Scope (1)
2/8/2024 14
Scope (2)
2/8/2024 15
Scope (3)
public class scope3 {
public static void main(String args[])
{
if (...) {
float x = 4.2;
[Link](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.
2/8/2024 16
Scope (4)
public class scope4 {
public static void main(String args[])
{
int x;
while (...) {
int x; // ERROR
} // end if
} // end main
} // end scope4
2/8/2024 17
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
2/8/2024 18
Scope (6)
public class scope6 {
public static void main(String args[]) {
if (...) {
int x = 3;
[Link](x);
} //end if
while (...) {
String x = "hello";
[Link](x);
} // end while
[Link](x); // error
} // end main
} // end scope6
2/8/2024 19
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
2/8/2024 20
Overloading
int sum(int a, int b) { //First version
return a+b;
}
int sum(int a, int b, int c) { //Second version
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) { //Third version
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
2/8/2024 21
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
2/8/2024 22