Functions and Methods
Functions (called methods in Java) are reusable blocks of code. They help organize logic, avoid repetition, and are essential for implementing algorithms.
Defining a Method
returnType methodName(parameters) { body }public static int add(int a, int b) {
return a + b;
}
Calling a Method
int result = add(5, 3); // result = 8
Method Overloading
Multiple methods with same name but different parameters.
public static int multiply(int a, int b) { return a * b; }
public static int multiply(int a, int b, int c) { return a * b * c; }
Passing by Value
Java passes primitives by value; for objects, the reference is passed by value (so object contents can be changed).
public static void modifyArray(int[] arr) {
arr[0] = 100; // changes the original array
}
Recursion
A method calling itself – key in DSA (trees, DP, etc.)
public static int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
Two Minute Drill
- Methods encapsulate reusable logic.
- Overloading allows same method name with different parameters.
- Java is pass-by-value (references passed by value).
- Recursion is a powerful technique in DSA.
- Use static for utility methods, instance for object behavior.
Need more clarification?
Drop us an email at career@quipoinfotech.com
