TutorialsICSEClass 10Computer Applications

User-Defined Methods in Java for ICSE Class 10: Parameters, Return and Overloading

ICSE Class 10 Computer Applications notes on user-defined methods: method prototype and signature, formal and actual parameters, return types, call by value vs call by reference, pure and impure methods, static methods and method overloading, with programs.

12 min read
User-Defined Methods in Java for ICSE Class 10: Parameters, Return and Overloading

User-defined methods (also called functions) let you split a program into small, reusable blocks. In the ICSE Class 10 board exam, they appear in Section A definitions and outputs, and in Section B overloading programs, which come up almost every year.

Parts of a method

public static int sum(int a, int b) {     // method header
    int s = a + b;                         // method body
    return s;                              // return statement
}
  • Access specifier: public, private or protected
  • Modifier: static (optional)
  • Return type: int, here. Use void if the method returns nothing.
  • Method name: sum
  • Parameter list: (int a, int b)

Method prototype: the first line of the method, public static int sum(int a, int b).

Method signature: the method name together with its parameter list, sum(int a, int b). Java uses the signature to tell overloaded methods apart. The return type is not part of the signature.

Formal and actual parameters

int r = sum(5, 7);     // 5 and 7 are ACTUAL parameters (arguments)
int sum(int a, int b)  // a and b are FORMAL parameters
  • Actual parameters are the values passed in the method call.
  • Formal parameters are the variables in the method definition that receive those values.

The return statement

  • A method can return only one value.
  • return ends the method immediately, and any statements after it are not executed.
  • A void method may use a bare return; to exit early.

Call by value vs call by reference

Call by value applies to primitive types (int, double, char, boolean). The method receives a copy, so changes inside it do not affect the caller:

static void change(int x) {
    x = 100;
}

int n = 5;
change(n);
System.out.println(n);    // 5: the original is unchanged

Call by reference applies to objects and arrays. The method receives a reference to the same object, so changes to its contents are visible to the caller:

static void change(int arr[]) {
    arr[0] = 100;
}

int a[] = {1, 2, 3};
change(a);
System.out.println(a[0]);    // 100

Pure and impure methods

  • A pure method does not change the state of the object (its data members). It usually just computes and returns a value. Example: a method that returns the area of a rectangle.
  • An impure method changes the state of the object. Example: a method that updates the balance in a BankAccount.

Static and non-static methods

  • A static method belongs to the class. You call it without an object, using ClassName.method() or just method() from inside the same class.
  • A non-static (instance) method needs an object: obj.method().

Method overloading

Overloading means defining several methods with the same name but different parameter lists: a different number, type or order of parameters. Java decides which one to run from the arguments in the call.

A different return type alone is not enough. int f(int x) and double f(int x) cannot both exist in one class.

Solved board-style program

Question: Design a class to overload a method series() as follows:

  • void series(int n): print the first n terms of 1, 4, 9, 16, …
  • double series(double x, int n): return the sum x + x² + x³ + … + xⁿ
  • void series(): print the Fibonacci series up to 10 terms
class Overload {
    void series(int n) {
        for (int i = 1; i <= n; i++)
            System.out.print(i * i + " ");
        System.out.println();
    }

    double series(double x, int n) {
        double sum = 0;
        for (int i = 1; i <= n; i++)
            sum += Math.pow(x, i);
        return sum;
    }

    void series() {
        int a = 0, b = 1;
        for (int i = 1; i <= 10; i++) {
            System.out.print(a + " ");
            int c = a + b;
            a = b;
            b = c;
        }
        System.out.println();
    }

    public static void main(String args[]) {
        Overload ob = new Overload();
        ob.series(5);                           // 1 4 9 16 25
        System.out.println(ob.series(2.0, 3));  // 2 + 4 + 8 = 14.0
        ob.series();                            // 0 1 1 2 3 5 8 13 21 34
    }
}

Practice questions

  1. What is the difference between a method prototype and a method signature?
  2. Give the output: static int f(int x) { return x * 2; } called as System.out.println(f(f(3)));
  3. Can two methods differ only in their return type? Why or why not?

Answers:

  1. The prototype is the whole first line, including the return type and access specifier. The signature is just the name and the parameter list.
  2. f(3) = 6, and f(6) = 12.
  3. No. The call would be ambiguous, because Java chooses an overloaded method by its parameters, not its return type.

Test yourself with the ICSE Class 10 MCQs on Methods and Constructors, then revise Constructors in Java.