FUNCTION IN JAVA
Function: block of code which is used to perform specific task.
Types of function:
1. User defined function- it is defined by user
Syntax:
Public int add(){
……………….
}
//Addition of two numbers using function
public class AdditionDemo {
//create a function
//parameters are a and b
public static int addition(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
//arguments are 5 and 10
int add=addition(5,10,23);
System.out.println("Addition of two numbers is: " + add);
Factorial of a number:
4! =4x3x2x1
F(n)= f(n)xf(n-1)f(n-2)……1 public class FactorialDemo {
public static int factorial(int n){
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
return fact;
}
public static void main(String[] args) {
int num= factorial(6);
System.out.println("Factorial of " + 6 + " is: " + num);
}
}
Recursion: when the function calls itself
n * factorial(n - 1);
n=6
6xfact(5)
5xfact(4)
4xfact(3)
3xfact(2)
2xfact(1)
1x1
public class FactorialDemo {
public static int factorial(int n){
if( n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
} else {
return n * factorial(n - 1); // Recursive case
}
public static void main(String[] args) {
int num= factorial(6);
System.out.println("Factorial of " + 6 + " is: " + num);
}
}
2. Built in function
#Built in Function in java:
1. Math function
Math.max(10, 20); // Returns 20
Math.min(10, 20); // Returns 10
Math.sqrt(25); // Returns 5.0
Math.pow(2, 3); // Returns 8.0
Math.abs(-7); // Returns 7
Math.random(); // Returns a random double between 0.0 and 1.0
public class MathFunction {
public static void main(String[] args) {
double number = 16.0;
// Square root
double sqrt = Math.sqrt(number);
System.out.println("Square root of " + number + " is " + sqrt);
// Power
double power = Math.pow(number, 2);
System.out.println(number + " raised to the power 2 is " +
power);
// Sine
double radians = Math.toRadians(30);
double sine = Math.sin(radians);
System.out.println("Sine of 30 degrees is " + sine);
// Absolute value
double negative = -25.5;
double absValue = Math.abs(negative);
System.out.println("Absolute value of " + negative + " is " +
absValue);
// Maximum
double max = Math.max(10, 20);
System.out.println("Maximum of 10 and 20 is " + max);
}
}
2. String Functions
String str = "Hello World";
str.length(); // Returns number of characters
str.toUpperCase(); // "HELLO WORLD"
str.toLowerCase(); // "hello world"
str.charAt(0); // 'H'
str.indexOf("World"); // 6
str.contains("lo"); // true
str.replace("World", "Java"); // "Hello Java"
public class StringFunction {
public static void main(String[] args) {
String str = "Hello, World!";
// Length of string
System.out.println("Length: " + str.length());
// Convert to uppercase
System.out.println("Uppercase: " + str.toUpperCase());
// Convert to lowercase
System.out.println("Lowercase: " + str.toLowerCase());
// Substring
System.out.println("Substring (0,5): " + str.substring(0, 5));
// Replace
System.out.println("Replace 'World' with 'Java': " +
str.replace("World", "Java"));
// Check if contains
System.out.println("Contains 'Hello': " + str.contains("Hello"));
// Character at index
System.out.println("Char at 1: " + str.charAt(1));
// Index of substring
System.out.println("Index of ',': " + str.indexOf(','));
}
}
3. Array Functions (java.util.Arrays)
int[] arr = {3, 5, 1, 4};
Arrays.sort(arr); // Sorts the array
Arrays.toString(arr); // Returns string representation: [1, 3, 4, 5]
Arrays.binarySearch(arr, 4); // Searches for 4 in array
4. Character Functions
Character.isDigit('5'); // true
Character.isLetter('A'); // true
Character.toUpperCase('a'); // 'A'