Page 353 - Computer_Science_F5
P. 353
Computer Science public static void main(String[] args) {
// Calling the public static function add from the MathCalculations class
int result = MathCalculations.add(230, 300);
System.out.println(“The sum of 230 and 300 is: “ + result);
FOR ONLINE READING ONLY
}
}
Output:
Figure 5.14: Demonstrate method call
(ii) Return types: Functions can optionally return a value (for example when
calculating the area of such as rectangle, triangle). When you need a function to
return value, the type of data must be specified (like ‘int’, ‘String’ or ‘double’),
as shown in the Program Example 5.14.
Program Example 5.14:
Java program to demonstrate return type function
public class AreaCalculator {
// Here a function computes the area of a square and returns the result
public static int computeSquareArea(int Length) {
return Length * Length;
}
public static void main(String[] args) {
// computeSquareArea function is called for printing answer
int squareArea = AreaCalculator.computeSquareArea(25);
System.out.println(“Area of square: “ + squareArea);
}
}
Output:
Figure 5.15: Demonstrate return type function
344
for Advanced Secondary Schools
Computer Science Form 5.indd 344 23/07/2024 12:34

