11 Handout 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

IT1708

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)
}

11 Handout 1 *Property of STI


Page 2 of 4
IT1708

The following example is a void function with empty parameters list:


public static void displayMessage() {
System.out.println(“This is an example void function that will display when invoked.”);
}
The following example is a void function with formal parameters:
public static void displayInfo(String fname, String lname, int age) {
System.out.println(“Student Information:”);
System.out.println(“First Name: ” + fname);
System.out.println(“Last Name: ” + lname);
System.out.println(“Age: ” + age);
}
The following example demonstrates how to define a function within a class and how to call it. Because void functions do not return a
value, they are not used in an expression:
public class VoidExamples {
public static void main (String[] args) {
//example statements of how to use a non-static function
VoidExamples msg = new VoidExamples();
msg.displayMessage();

displayInfo(“Jess”, “Diaz”, 19);


}
//a non-static function with empty parameters list
public void displayMessage () {
System.out.println(“This is an example void function that will display when invoked.”);
}
//a static function with parameters list
public static void displayInfo (String fname, String lname, int age) {
System.out.println(“Student Information:”);
System.out.println(“First Name: ” + fname);
System.out.println(“Last Name: ” + lname);
System.out.println(“Age: ” + age);
}
}
Sample Output:
This is an example void function that will display when invoked.
Student Information:
First Name: Jess
Last Name: Diaz
Age: 19
Parameter Passing
Java passed arguments, such as int into functions by value. This means that any changes to the values of the parameters exist only
within the scope of the function. When a function returns, the parameters are gone and any changes to them are lost. The program below
shows an example of passing parameter by value. The values of the arguments remain the same even after the method invocation:
public class PassByValue {
public static void main (String[] args) {
int x = 3;
passMethod(x); //invoke passMethod with x as argument
System.out.println("After invoking passMethod, x = " + x);
}
//change parameter in passMethod()
public static void passMethod (int x) {
x = 10;
System.out.println("The value of x inside the passMethod is " + x);
}
}
Sample Output:
The value of x inside the passMethod() is 10
After invoking passMethod, x = 3
REFERENCES:
Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th edition. Boston: Course Technology, Cengage Learning.
Malik, D.S. (2012). Java programming: From problem solving to program design, 5th edition. Boston: Cengage Learning.
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th edition. California: Pearson Education, Inc.

11 Handout 1 *Property of STI


Page 3 of 4

You might also like