ICSE Class 10 Java - Functions / Methods Detailed
Notes
Introduction to Functions
A function (method in Java) is a block of code designed to perform a particular task. It is defined
within a class and invoked to execute a set of statements. Benefits: - Modularity: Code is divided
into independent blocks. - Reusability: Functions can be reused in multiple programs. - Readability:
Improves understanding and debugging. - Testing: Easier to test smaller modules. Syntax:
returnType methodName(parameterList) { // method body return value; // if returnType is not void }
Examples in daily life: - A coffee machine has functions like grindBeans(), brewCoffee(), addMilk(). -
A mobile phone has methods like call(), message(), takePhoto().
Types of Functions in Java
Java methods can be classified into two broad types: 1. User-defined methods: Created by the
programmer for specific tasks. 2. Library methods: Built-in methods provided by Java libraries (e.g.,
Math.sqrt()). User-defined methods are further categorized into 4 types based on parameters and
return values: 1. With return & with parameters 2. With return & without parameters 3. Without
return & with parameters 4. Without return & without parameters Example analogy: - A bank's
'deposit' method requires parameters (amount) and updates balance (return new balance). - A
clock's 'showTime' method has no parameters but returns the current time.
Pure and Impure Functions
Pure Functions: - Always return the same result for the same input. - Do not modify object or class
variables. - Example: Math.sqrt(25) always returns 5.0. Impure Functions: - May modify the state of
objects or global variables. - Example: deposit(amount) modifies account balance. Real-life
analogy: - A calculator's squareRoot() button is pure (same input gives same output). - A vending
machine's dispenseItem() is impure (state changes as items are dispensed).
Method Header and Signature
The method header (declaration) includes: - Access specifier (public, private) - Return type (int,
double, void, etc.) - Method name (identifier) - Parameter list (optional) Example: public int add(int
a, int b) Method Signature: - The combination of method name and parameter list. - Return type is
not part of the signature. - Important for method overloading. Analogy: - Person’s name + phone
number = unique identity (like method signature). - Address or profession (return type) does not
affect the signature.
Advantages and Disadvantages
Advantages of using functions: - Code Reuse: Write once, use multiple times. - Reduces
redundancy. - Simplifies complex problems into smaller subproblems. - Easier testing and
debugging. - Encourages top-down approach. Disadvantages: - Function calls add overhead
(time/memory usage). - Too many small functions may reduce readability. Real-life example: - A
school has specialized teachers (functions) for each subject. This increases efficiency but also adds
coordination overhead. Conclusion: Functions are the foundation of modular programming in Java
and are essential for problem solving, organizing code, and building large applications.