User-Defined Methods
ICSE Class 10 · Computer Applications · Chapter 3
What this chapter covers
- Need for methods
- Syntax of a method, method prototype and signature
- Forms of methods: ways to define a method
- Method definition and method calling
- Returning values from methods
- Static and non-static methods
- Object creation and modular programming with multiple methods
- Call by value (pass by value)
- Call by reference (pass by reference)
- Pure and impure methods
- Method overloading (polymorphism)
Key points
- A user-defined method is a named block of statements, written by the programmer, that performs one task and is called when needed.
- Methods give reusability, less repetition, easier debugging and modular programming.
- A method header contains an optional access specifier and modifier, a compulsory return type, the method name and the parameter list.
- The prototype is the complete first line of a method; the signature is only the name with the number, types and order of parameters.
- Methods take four forms, depending on whether they receive parameters and whether they return a value.
- Actual parameters appear in the call; formal parameters appear in the header and receive copies of the actual values, matched by position and type.
- `return` ends a method and sends back exactly one value of the declared type; a `void` method returns nothing and is called as a statement.
- Static methods belong to the class and need no object; non-static methods belong to objects and are called as `object.method()`.
- Data members store an object's state; member methods work on them; `new` creates the object through which member methods are invoked.
- Call by value passes a copy, so the caller's variable is unchanged; Java passes every primitive type this way.
- Call by reference passes the reference, so changes inside the method are seen by the caller; in Java this happens with objects and arrays.
- A pure method does not change the state of an object; an impure method changes it.
- Method overloading means several methods with the same name but different parameter lists; it is compile-time polymorphism, and a different return type alone does not make an overload.
Questions answered
Define a user-defined method and give two reasons for using methods in a program.
Distinguish between a method prototype and a method signature, using `public static int sum(int a, int b)` as the example.
Write the prototype of a method `isPalindrome` that accepts an integer and returns `true` or `false`.
Identify the actual and formal parameters in: `double avg(int x, int y) { return (x + y) / 2.0; }` called as `double r = avg(m1, m2);`.
What is the value returned by `avg(7, 8)` in Question 4, and why is `2.0` used instead of `2`?
Why does the statement `int x = display();` give an error if `display()` is declared as `void display()`?
Give the output, and state which way of passing is used. `static void inc(int n) { n++; }` with `int a = 5; inc(a); System.out.println(a);`
Why can `main()` call `Math.sqrt()` without creating an object, but not a non-static method of its own class?
Past board exam questions
ICSE 2026 board paper · 15 marks
Define a class to overload the method format as follows: void format (): To print the following pattern using Nested for loops only: 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 int format (String s): To calculate and return the sum of ASCII codes of each character of the String. Example: CAB Output: 67 + 65 + 66 198 void format (int n): To calculate and display the sum of natural numbers up to n given by the formula. (n(n+1))/(2)
Show answer
ICSE 2026 board paper · 1 mark
In the following method prototype to accept a character, an integer and return YES or NO, fill in the blank to complete the method prototype. public ________ someMethod (char ch, int n)
- boolean
- String
- int
- double
Show answer
ICSE 2026 board paper · 1 mark
In a calculator which Java feature allows multiple methods named calculate() for the different operations?
- abstraction
- inheritance
- encapsulation
- polymorphism
Show answer
ICSE 2025 compartment paper · 15 marks
Define a class to overload the method transform as follows: int transform(int n) – to return the sum of the digits of the given number Example: n = 458 output : 17 void transform(String s) – to convert the given String to upper case and print Example: if S = "Blue" Output : BLUE void transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops. Example: if ch = '@' Output : @@@ @@@ @@@
Show answer
ICSE 2025 board paper · 15 marks
Define a class to overload the method print( ) as follows: void print ( ) – To print the given format using nested loops. ``` @#@#@ @#@#@ @#@#@ @#@#@ ``` double print(double a, double b) – To display the sum of numbers between a and b with difference of 0.5. e.g. if a = 1.0, b = 4.0 output is: 1.0 + 1.5 + 2.0 + 2.5 + ……………. + 4.0 int print(char ch1, char ch2) – compare the two characters and return the ASCII code of the largest character.
Show answer
ICSE 2025 sample paper · 15 marks
Define a class to overload the method display() as follows: void display(): To print the following format using nested loop. ``` 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 ``` void display (int n, int m) : To print the quotient of the division of m and n if m is greater than n otherwise print the sum of twice n and thrice m. double display (double a, double b, double c) – to print the value of z where z = p × q p = (a+b)/(c) q = a + b + c
Show answer
ICSE 2025 compartment paper · 1 mark
Which statement is correct for the method prototype given below: int check(char ch, String s)
- check( ) does not return any value
- check( ) has return type int
- check method has two actual parameters
- check( ) is a constructor
Show answer
ICSE 2025 compartment paper · 1 mark
The statement given below is termed as: pubic void Accept(int a)
- Method signature
- Method block
- Method prototype
- Constructor