Methods / Functions (Class 10)
_____________________________________________
● A group of statements or instructions written to perform a particular task, is
known as Method or Function.
● There are two types of methods, such as :
(1) In-built Method or Pre-defined Method
(2) User-defined method
● In the case of a predefined method just like Math.pow() we have to give some
inputs and we will get the desired output and we can’t customize these.
● In the user-defined method, we can write instructions as per our choice/need
and can return the desired output.
● Syntax for User-defined method →
<Access Specifier> <Return type> <Method name> (Parameters)
{
Statements;
…..
Return (value);
}
● Example →
double area(int x)
{
double a=x*x;
return (a);
}
● Components of User-Defined method →
1.Method header / Prototype
(Includes return type, method name and parameters)
2.Method Signature
(Includes method name and parameters(if any))
3.Access Specifier
(Public/Private/Protected)
If nothing is mentioned, then by default the method is Public.
4.Body of a method
Anything that is written inside the method.
5.Parameter list
The input values the method is taking to perform the task.
6.Return Statement and Return type
Return type means the data type of the final output the method is
returning.
Return Statement is the last line of a method. We use the keyword
“return”.
●Features of a Return Statement →
1.It is given at the end of the method block.
2.No statement in the method body can be executed after the return
statement.
3.A method can return only one value to its caller.
4.In case of more than one return statement, the first one gets
executed.
●Invoking or Calling of a method →
➢Using an object (Non-static Methods)
Class A{
public static void main(String args[])
{
A ob=new A();
Double a=ob.area(3);
System.out.print(“The area of the square is =”+a);
}
public double area(int x)
{
Double y=x*x;
Return y;
}
}
➢Without using an object (Static Methods)
Class A{
public static void main(String args[])
{
Double a=area(3);
System.out.print(“The area of the square is =”+a);
}
public static double area(int x)
{
Double y=x*x;
Returny;
}
}
Static Methods Non-Static Methods
(1)These methods are associated with (1)These methods are associated with
the class. the instance of a class.
(2)To call these methods we don’t (2) to call these methods we have to
have to create object. create the object of the class.
(3) Memory is allocated only once for the (3) Memory allocated each time an instance
entire program execution. is created.
●Types of Parameters →
1.Actual Parameters
2.Formal Parameters
In the above mentioned code, the ‘3’ in the caller method is the
Actual parameter. The ‘x’ in the calling method is the Formal
Parameter.
●Different ways of defining a method →
1.Parameterized way
Receiving value and returning outcome to the caller.
Receiving value and not returning the outcome.
2.Non-parameterized way
Neither Receiving value nor returning outcome to the caller.
Not Receiving value and returning the outcome.
______________________________________________________
Parameterized method (Example with code)
Code 1
Write a class using a function int num(int) that accepts a number and
finds whether it is ODD and divisible by 5 or not. It returns 1 if the
condition is satisfied otherwise 0. Use a main method to pass the
number by value to the function.
Class A{
Public static int num(int a)
{
if(a%2!=0 && a%5==0)
{
Return 1;
}
Else
{
Return 0;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the number =”);
Int number=sc.nextInt();
Int x=num(number);
system.out. println(x);
}
Code 2
Write a program to accept a number and check whether the number is
palindrome or not by using function name rev(int n). The function
returns the reversed number to the main function that checks the
palindrome number.
Class A{
Public static int rev(int n)
{
Int digit=0,rev=0;
while(n>0)
{
digit=n%10;
rev=rev*10+digit;
n=n/10;
}
Return (rev);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the number =”);
Int number=sc.nextInt();
Int x=rev(number);
if(x==number)
{
System.out.println(“It is a palindrome number”);
}
Else
{
System.out.println(“It is a palindrome number”);
}
}
}
Code 3 (Homework)
Write a program to accept a number and how many 1s are there in the
number by using function name count(int n). Print the result accordingly
using that method and call it from the main method to get the output.
Code 4
Write a program to accept a number. Calculate and display its factorial
using function fact(int).
Class A{
Public static void fact(int a)
{
Int fact=1;
for(int i=1; i<=a; i++)
{
fact*=i;
}
System.out.println(“The factorial of “+a+” is ”+fact);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the number =”);
Int number=sc.nextInt();
fact(number);
}
}
Code 5
Write a program to input a number and check and print whether it is a 'Pronic' number
or not. Use a function int Pronic(int n) to accept a number. The function returns 1, if
the number is 'Pronic', otherwise returns zero (0).
(Hint: Pronic number is the number which is the product of two consecutive integers)
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
import java.util.Scanner;
public class PronicNumber
public int pronic(int n) {
int isPronic = 0;
for (int i = 1; i <= n - 1; i++) {
if (i * (i + 1) == n) {
isPronic = 1;
break;
return isPronic;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();
PronicNumber obj = new PronicNumber();
int r = obj.pronic(num);
if (r == 1)
System.out.println(num + " is a pronic number");
else
System.out.println(num + " is not a pronic number");
Code 6
Write a program to enter a two digit number and find out its first factor excluding 1
(one). The program then find the second factor (when the number is divide by the
first factor) and finally displays both the factors.
Hint: Use a non-return type function as void fact(int n) to accept the number.
Sample Input: 21
The first factor of 21 is 3
Sample Output: 3, 7
Sample Input: 30
The first factor of 30 is 2
Sample Output: 2, 15
import java.util.Scanner;
public class Factors
public void fact(int n) {
if (n < 10 || n > 99) {
System.out.println("ERROR!!! Not a 2-digit number");
return;
int i;
for (i = 2; i <= n; i++) {
if (n % i == 0)
break;
int sf = n / i;
System.out.println(i + ", " + sf);
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
Factors obj = new Factors();
obj.fact(num);
____________________________________________________________
Non - Parameterized method (Example with code)
__________________________________________
Code 1
Write a program to use a function Prime() to input and check whether
the entered number is a prime number or not.
Class A{
Public static void prime()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the number =”);
Int number=sc.nextInt();
Int count=0;
for(int i=1; i<=a; i++)
{
if(a%i==0)
{
count+=1;
}
}
if(count==2)
{System.out.println(“Prime number”);}
else
{System.out.println(“Not Prime number”);}
}
public static void main(String args[])
{
Prime ();
}
}
Code 2 (Homework)
Write a program to use a function Prime() to input and check whether
the entered number is a prime number or not. If the number is Prime the
function returns 1 else 0 to the main function.
_____________________________________________________________
Difference between :
* Example - Write one code and show the actual and formal parameters in that
code.
Example →
Public class A{
Static int count=0; //Global variable
static void pure(int x)
{
System.out.println(x*x);
}
static void impure(int x)
{
count+=x;
System.out.println(count);
}
Public static void main(String[] args)
{
pure(5);
impure(5);
}
_______________________________________________________________
Call by Value and Call by Reference
_____________________________________________
Call by value Call by reference
The copy of the actual parameters is sent The actual parameters and Formal
to the formal parameter. parameters share the same location in
the memory.
Any change in the formal parameter will Any change in the formal parameter, will
not reflect on the actual parameter. change the actual parameter also.
In this, only primitive data type can be In this, only non-primitive data type can
used. be used.
Example for Call by Reference →
Public class A{
static int[ ] arrayModify(int x[ ])
{
for(int i =0; i<4; i++)
{
x[i]+=5;
}
Return x;
}
Public static void main(String[] args)
{
Int[ ] a={1,2,3,4};
Int[ ] b=arrayModify(a);
for(int i =0; i<4; i++)
{
System.out.print(b[i]+” ”);
}
}
}
Method Overloading ( Polymorphism )
____________________________________________
● Method overloading in Java is also known as Compile-time Polymorphism,
Static Polymorphism, or Early binding, because the decision about which
method to call is made at compile time.
Features of Method Overloading →
● Multiple methods can share the same name in a class when their
parameter lists are different.
● Overloading is a way to increase flexibility and improve the readability of
code.
● Overloading does not depend on the return type of the method, two
methods cannot be overloaded by just changing the return type.
Code - 1
Write a class with the name Perimeter using function overloading that
computes the perimeter of a square, a rectangle and a circle.
Formula:
Perimeter of a square = 4 * s
Perimeter of a rectangle = 2 * (l + b)
Perimeter of a circle = 2 * (22/7) * r
import java.util.Scanner;
public class Perimeter
{
public double perimeter(double s) {
return 4 * s;
}
public double perimeter(double l, double b) {
return 2 * (l + b);
}
public double perimeter(int c, double pi, double r) {
return c * pi * r;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Perimeter obj = new Perimeter();
System.out.print("Enter side of square: ");
double side = in.nextDouble();
System.out.println("Perimeter of square = " + obj.perimeter(side));
System.out.print("Enter length of rectangle: ");
double l = in.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = in.nextDouble();
System.out.println("Perimeter of rectangle = " + obj.perimeter(l, b));
System.out.print("Enter radius of circle: ");
double r = in.nextDouble();
System.out.println("Perimeter of circle = " + obj.perimeter(2, 3.14159,
r));
}
}
Code - 2
Design a class overloading a function calculate() as follows:
1. void calculate(int m, char ch) with one integer argument and one
character argument. It checks whether the integer argument is divisible
by 7 or not, if ch is 's', otherwise, it checks whether the last digit of the
integer argument is 7 or not.
2. void calculate(int a, int b, char ch) with two integer arguments and one
character argument. It displays the greater of integer arguments if ch is
'g' otherwise, it displays the smaller of integer arguments.
import java.util.Scanner;
public class Calculate
{
public void calculate(int m, char ch) {
if (ch == 's') {
if (m % 7 == 0)
System.out.println("It is divisible by 7");
else
System.out.println("It is not divisible by 7");
}
else {
if (m % 10 == 7)
System.out.println("Last digit is 7");
else
System.out.println("Last digit is not 7");
}
}
public void calculate(int a, int b, char ch) {
if (ch == 'g')
System.out.println(a > b ? a : b);
else
System.out.println(a < b ? a : b);
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Calculate obj = new Calculate();
System.out.print("Enter a number: ");
int n1 = in.nextInt();
obj.calculate(n1, 's');
obj.calculate(n1, 't');
System.out.print("Enter first number: ");
n1 = in.nextInt();
System.out.print("Enter second number: ");
int n2 = in.nextInt();
obj.calculate(n1, n2, 'g');
obj.calculate(n1, n2, 'k');
}
}
Code - 3
Write a program using a function called area() to compute area of the
following:
(a) Area of circle = (22/7) * r * r
(b) Area of square= side * side
(c) Area of rectangle = length * breadth
** [Run each code using your logic (Don’t just copy and paste
my logic) and practice more and more]