Methods
Date @October 7, 2022
Introduction to Java Programming and Data Structures, Comprehensive
Text Version: Chapter 6
Defining Methods
A method is a collection of statements that are grouped together to perform an
operation
// define the method
public static int max(int num1, int num2) {
int result;
if (num1>num2) {
result = num1;
} else {
result = num2
}
return result;
}
// invoke the method
int z = max(x, y);
public static int max(int num1, int num2) - method header
public static - modifier
int - returnValueType
the data type of the value that is returned by the method
if the method does not return a value, the returnValueType is the keyword void
max - method name
(int num1, int num2) - method signature
combination of the method name and the parameter list
int num1, int num2 - parameter list
Methods 1
, num1 & num2 - formal parameters
variables defined in the method header
return result; - return value
x & y - actual parameters (arguments)
the value passed on to the parameter when a method is invoked
Calling Methods
// define the method
public static int max(int num1, int num2) {
int result;
if (num1>num2) {
result = num1;
} else {
result = num2
}
return result;
}
public static void main (String[] args) {
int i = 5;
int j = 2;
int k = max(i, j)
System.out.println("The maximum between " + i + " and " + j + " is " + k);
}
this program takes the integer values of i and j and puts them through the max
method,
caution:
// section a
public static int sign(int n) {
if (n > 0) {
return 1;
} else if (n == 0) {
return 0;
} else if (n < 0) {
return -1;
}
}
Methods 2
Date @October 7, 2022
Introduction to Java Programming and Data Structures, Comprehensive
Text Version: Chapter 6
Defining Methods
A method is a collection of statements that are grouped together to perform an
operation
// define the method
public static int max(int num1, int num2) {
int result;
if (num1>num2) {
result = num1;
} else {
result = num2
}
return result;
}
// invoke the method
int z = max(x, y);
public static int max(int num1, int num2) - method header
public static - modifier
int - returnValueType
the data type of the value that is returned by the method
if the method does not return a value, the returnValueType is the keyword void
max - method name
(int num1, int num2) - method signature
combination of the method name and the parameter list
int num1, int num2 - parameter list
Methods 1
, num1 & num2 - formal parameters
variables defined in the method header
return result; - return value
x & y - actual parameters (arguments)
the value passed on to the parameter when a method is invoked
Calling Methods
// define the method
public static int max(int num1, int num2) {
int result;
if (num1>num2) {
result = num1;
} else {
result = num2
}
return result;
}
public static void main (String[] args) {
int i = 5;
int j = 2;
int k = max(i, j)
System.out.println("The maximum between " + i + " and " + j + " is " + k);
}
this program takes the integer values of i and j and puts them through the max
method,
caution:
// section a
public static int sign(int n) {
if (n > 0) {
return 1;
} else if (n == 0) {
return 0;
} else if (n < 0) {
return -1;
}
}
Methods 2