11 Handout 1
11 Handout 1
11 Handout 1
Pre-defined Functions
A function or method is a block of statements that are grouped together to perform an operation. Functions are reusable and used to
enhance the program’s readability. There are two (2) types of functions in programming: pre-defined functions, functions that are already
written and provided by the programming language, and user-defined functions, functions that are created by programmers.
In Java, pre-defined functions are organized as a collection of classes, called class libraries. For example, the class Math contains
mathematical methods. To use pre-defined functions of a class in a program, you must import the class from the package containing the
class. For example, to use the method nextInt of the class Scanner contained in the package java.util, we import this class from
the package java.util:
import java.util.Scanner;
All classes in the package java.lang are imported by default. Table 1 lists some of Java’s pre-defined mathematical methods of the
class Math in the package java.lang. The methods in the Math class are static methods, which means they are used with the class
name without instantiating a Math object.
Table 1: Some Pre-Defined Mathematical Methods of Class Math (Malik, 2012)
Consider the following declarations:
double a = 3, b = 23, c = 34.45, d = 65.78;
int e = -57;
Method Description Example
abs(x) Returns the absolute value of x. Math.abs(d); //returns the value of 57
Returns a value of x of type double, which is Math.ceil(c); //returns the value of 35.0
ceil(x) the smallest integer value that is not less than Math.ceil(d); //returns the value of 66.0
x.
Returns Euler’s number e (ex) raised to the Math.exp(3) //returns the value of
exp(x) power of a double value, where e is 20.085536923187668
approximately 2.7182818284590455.
Returns a value of x of type double, which is Math.floor(c); //returns the value of 34.0
floor(x)
the largest integer value less than x. Math.floor (d); //returns the value of 65.0
Returns a value of x of type double, which is Math.log(a); //returns the value of
log(x)
the natural logarithm (base e) of x. 1.0986122886681098
Returns a value of x of type double, which is Math.log10(a); //returns the value of
log10(x)
the common logarithm (base 10) of x. 0.47712125471966244
Returns the largest of x and y. If either of x and Math.max(a, b); //returns the value of 23.0
max(x, y) y is of type double, it returns a value of type Math.max(12, a); //returns the value of 12.0
double.
Returns the smallest of x and y. Math.min(a, b); //returns the value of 3.0
min(x, y)
Math.min(23, 12); //returns the value of 12
Returns a value of type double, which is the Math.pow(2, a); //returns the value of 8.0
pow(x, y) value of the first argument raised to the power
of the second argument (xy).
Returns a value of long type that is the greater Math.round(c); //returns the value of 34
round(x)
closest to x. Math.round(d); //returns the value of 66
Returns a value of double type, which is the Math.sqrt(4); //returns the value of 2.0
sqrt(x)
square root of x.
Returns the cosine of x measured in radians. Math.cos(34); //returns the value of
cos(x)
-0.8485702747846052
Returns the sine of x measured in radians. Math.sin(34); //returns the value of
sin(x)
0.5290826861200238
Returns the tangent of x measured in radians. Math.tan(34); //returns the value of
tan(x)
-0.1425465430742778
When a method returns a value, the method can be used in an expression, for example, int x = Math.abs(-57); // the return
value is stored in variable x.
When you use a method, you are said to invoke or call it. For example, your program invoked the method nextInt using objects of the
class Scanner.
User-defined Functions
User-defined functions in Java are classified into two (2) categories:
• Value-returning functions – functions that return a value of specific data type using the return statement. These functions are
used to save the value for further calculations.
• Void functions – functions that do not return a value and not using the return statement.
11 Handout 1 *Property of STI
Page 1 of 4
IT1708
VALUE-RETURNING FUNCTIONS
When creating functions, they should be within a class in any order. The following is the general syntax of a value-returning function:
modifiers returnType methodName (formal parameter list) {
statement(s)
return expression;
}
In this syntax:
• modifiers indicate the visibility of the method, that is wherein a program the method can be used. Some of the most used
modifiers are public, private, and static keywords. The modifier public specifies that the method can be invoked outside
the class. The modifier private specifies that the method cannot be used outside the class. The static keyword is used to specify
that the method can be invoked by using the name of the class and can be called within the method main or another method of the
class containing the application program.
• returnType is the type of the value that the method returns.
• methodName is a Java identifier, giving a name to the method or function.
• formal parameter list is the list of parameters, it is the type, order, and number of parameters of a method. The parameters
are separated by commas and can be more than one (1) parameter. These are optional, a function’s formal parameter list can be
empty, but the parentheses are still needed, for example, methodName(). Parameters provide communication link between the
calling method and the called method. They enable methods to manipulate different data each time they are called.
• Statements enclosed between braces form the body of the method. The return statement is used to return the value of the
function. It passes a value back when the function is called or invoked. The data type of the value to return should be compatible
with the return type of the method.
The following example has a method named sumOfTwoInt with return type int and the data type of value to return is int, which is
the variable sum. The method has two (2) parameters, the x and y of int type.
public static int sumOfTwoInt (int x, int y) {
int sum = x + y;
return sum;
}
In the given example, the public static int sumOfTwoInt (int x, int y) is called the method heading and the method body
is the statements within the braces. To call a value-returning function, you use its name, with the actual parameters in parentheses:
sumOfTwoInt(3, 7);
int num = sumOfTwoInt(10, 5); //the static function returns 10 and store in in variable num.
When calling a function, the number of actual parameters together with their data types must match the formal parameters in order
given in the function.
The process of method calling is when a program invokes a method, the program control gets transferred to the called method. This
called method then returns control to the caller. The following example demonstrates how to define a function within a class and how to
call it:
public class SumCalculator {
public static void main (String[] args) {
int a = 3, b = 7;
int num;
num = sumOfTwoInt(a, b);
System.out.println(“The sum of 3 and 7 is ” + num);
}
//a static function that sum up two (2) integers
public static int sumOfTwoInt(int x, int y) {
int sum;
sum = x + y;
return sum;
}
}
Sample Output:
The sum of 3 and 7 is 10
VOID FUNCTIONS
The keyword void is used to create void functions. The following is the general syntax of a void function:
modifiers void methodName (formal parameter list) {
statement(s)
}