KEMBAR78
Java methods or Subroutines or Functions | PDF
User defined Subroutines / Methods
/ Functions
in Java
Dr. Kuppusamy .P
Associate Professor / SCOPE
User defined methods in Java
• Method is a collection of statements grouped together to perform an
operation.
• User can also define methods inside a class called user-defined
methods or subroutines or functions.
• An user defined method will run only when it is called.
• User can pass data, known as parameters / arguments, into a
method.
• Why are methods needed?
• Code Reusability: Define the code once, use it many times.
Dr. Kuppusamy P
Creating Method
• Method definition consists of a method header and a method body.
Syntax
modifier returnType nameOfMethod (Parameter List)
{
// body with Set of Statements
}
• modifier − defines the access type of the method and it is optional.
• returnType − Method may return a value or void and it is mandatory.
• nameOfMethod − method name is any user defined name.
• Parameter List − The list of parameters, data type, order, and number of
parameters. These are optional, method may contain zero parameters.
• method body − Set of statements perform a specific task.
Dr. Kuppusamy P
Method - Example
public static void findFact () // Method Definition
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
System.out.println(f);
}
// Method calling
public static void main(String args[])
{
findFact();
} Dr. Kuppusamy P
Creating Method
Method can be created in the following four ways
1. Method without return-type without parameters
2. Method without return-type with parameters
3. Method with return-type without parameters
4. Method with return-type with parameters
5. Static methods
6. Non-static methods
Dr. Kuppusamy P
Creating Method - Example
1. Method without return-type without parameters
public class FactEx
{
public void Fact () // Method Definition
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
System.out.println(f);
}
// Method calling
public static void main(String args[])
{
Fact();
}
} Dr. Kuppusamy P
Creating Method - Example
2. Method without return-type with parameters
public class FactEx1
{
public void Fact (int n) // Method Definition
{
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
System.out.println(f);
}
// Method calling
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
Fact(n);
}
} Dr. Kuppusamy P
Creating Method - Example
3. Method with return-type without parameters
public class FactEx2
{
public int Fact() // Method Definition
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
return f;
}
// Method calling
public static void main(String args[])
{
int factorial = Fact();
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Creating Method - Example
4. Method with return-type with parameters
public class FactEx3
{
public int Fact (int n) // Method Definition
{
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
return f;
}
// Method calling
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
int inp = sin.nextInt();
int factorial = Fact(inp);
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Creating Method - Example
5. Static methods
public class StaticEx {
public static int Fact (int n) //method definition
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public static void main(String[] args)
{
Scanner sin=new Scanner(System.in);
int num = sin.nextInt();
int factorial = Fact(num); //method calling
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Creating Method - Example
6. Non-static methods
public class NonStaticEx {
public int Fact (int n) //method definition
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public static void main(String[] args)
{
Scanner sin=new Scanner(System.in);
int num = sin.nextInt();
NonStaticEx m = new NonStaticEx(); //object or instance creation for the class NonStaticEx
int factorial = m.Fact(num); //method calling
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Passing arrays as Parameters
Dr. Kuppusamy P
Passing arrays as Parameters
• Pass arrays to a method like normal variables.
• When pass an array as an argument, actually the address of the
array in the memory is passed (reference).
Creating Method with Arrays
i. Without return an array
modifier returnType Methodname (data-type array-name[])
{
// method body
}
ii. With return an array
modifier data-type[] Methodname (data-type array-name[])
{
// method body
}
Dr. Kuppusamy P
Example
import java.util.*;
public class ArrayPass
{
public int min(int [] array)
{
int min = array[0];
for(int i=0; i<array.length; i++)
{
if(array[i] < min)
{
min = array[i];
}
}
return min ;
}
Dr. Kuppusamy P
Example
public static int[] descending(int [] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array ;
}
Dr. Kuppusamy P
Example
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int[] myArray = new int[size];
for(int i=0; i<size;i++)
{
myArray [i] = sc.nextInt();
}
ArrayPass m=new ArrayPass ();
int max= m.min(myArray);
System.out.println(max);
int dsc[] = m.descending(myArray);
for(int i:dsc)
System.out.println(i);
}
} Dr. Kuppusamy P
Method Overloading in java
Dr. Kuppusamy P
Method Overloading in java
• Method Overloading is a class contains more than one method with the same method
name. But varying the argument lists like count, data type and arguments sequence.
• Example:
• Addition of two number for integer as well as float.
• Advantages
• Method overloading increases the readability of the program.
Different approaches of method overloading
1. Number of parameters.
• add(int a, int b)
• add(int a, int b, int c)
2. Data type of parameters.
• add(int a, int b)
• add(int a, float b)
3. Sequence of Data type of parameters.
• add(int, float)
• add(float, int)
Dr. Kuppusamy P
Method Overloading in java
import java.util.Scanner;
public class Overload
{
public static int add(int a, int b)
{
return a+b;
}
public static float add(float a, float b)
{
return a+b;
}
public static void add(float a, int b)
{
System.out.println(a+b);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int isum=add(num1,num2);
float fsum=add(34.34f,34.34f);
add(10.0f, 20);
System.out.println(fsum);
}
}
Dr. Kuppusamy P
Recursion in java
Dr. Kuppusamy P
Recursion in java
• Recursion is a process in which a method calls itself continuously.
• A method in java that calls itself is called recursive method.
• It makes the reusability of code. So, reduces the program size.
• Syntax
returntype methodname()
{
statements;
methodname();
}
Dr. Kuppusamy P
Factorial using recursion in java
• Example
import java.util.*;
public class RecFact
{
public static int fact(int n)
{
if (n <= 1)
{
System.out.println(n);
return 1;
}
else
{
System.out.print( n+ " * ");
return n * fact(n-1);
}
}
public static void main(String[] args)
{
int n;
System.out.println("Enter the no to find
factorial " );
Scanner sin = new Scanner(System.in);
n = sin.nextInt();
int res = fact(n);
System.out.println("Factorial " + res);
}
}
Dr. Kuppusamy P
Fibonacci using Recursion in java
• Example
import java.util.*;
public class FibRec{
public static int fib( int n)
{
if(n == 0)
{
return 0;
}
if(n==1 || n==2)
{
return 1;
}
else
return fib(n-2) + fib(n-1);
}
public static void main(String [] arg)
{
int n;
Scanner sin = new Scanner(System.in);
System.out.println("Enter the fib
series");
n = sin.nextInt();
for(int i=0; i<=n; i++)
{
System.out.print(fib(i)+ " ");
}
}
}
Dr. Kuppusamy P
References
Dr. Kuppusamy P
Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition,
2017.

Java methods or Subroutines or Functions

  • 1.
    User defined Subroutines/ Methods / Functions in Java Dr. Kuppusamy .P Associate Professor / SCOPE
  • 2.
    User defined methodsin Java • Method is a collection of statements grouped together to perform an operation. • User can also define methods inside a class called user-defined methods or subroutines or functions. • An user defined method will run only when it is called. • User can pass data, known as parameters / arguments, into a method. • Why are methods needed? • Code Reusability: Define the code once, use it many times. Dr. Kuppusamy P
  • 3.
    Creating Method • Methoddefinition consists of a method header and a method body. Syntax modifier returnType nameOfMethod (Parameter List) { // body with Set of Statements } • modifier − defines the access type of the method and it is optional. • returnType − Method may return a value or void and it is mandatory. • nameOfMethod − method name is any user defined name. • Parameter List − The list of parameters, data type, order, and number of parameters. These are optional, method may contain zero parameters. • method body − Set of statements perform a specific task. Dr. Kuppusamy P
  • 4.
    Method - Example publicstatic void findFact () // Method Definition { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); int f=1; for(int i=1;i<=n;i++) f=f*i; System.out.println(f); } // Method calling public static void main(String args[]) { findFact(); } Dr. Kuppusamy P
  • 5.
    Creating Method Method canbe created in the following four ways 1. Method without return-type without parameters 2. Method without return-type with parameters 3. Method with return-type without parameters 4. Method with return-type with parameters 5. Static methods 6. Non-static methods Dr. Kuppusamy P
  • 6.
    Creating Method -Example 1. Method without return-type without parameters public class FactEx { public void Fact () // Method Definition { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); int f =1; for(int i=1;i<=n;i++) f=f * i; System.out.println(f); } // Method calling public static void main(String args[]) { Fact(); } } Dr. Kuppusamy P
  • 7.
    Creating Method -Example 2. Method without return-type with parameters public class FactEx1 { public void Fact (int n) // Method Definition { int f =1; for(int i=1;i<=n;i++) f=f * i; System.out.println(f); } // Method calling public static void main(String args[]) { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); Fact(n); } } Dr. Kuppusamy P
  • 8.
    Creating Method -Example 3. Method with return-type without parameters public class FactEx2 { public int Fact() // Method Definition { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); int f =1; for(int i=1;i<=n;i++) f=f * i; return f; } // Method calling public static void main(String args[]) { int factorial = Fact(); System.out.println(factorial); } } Dr. Kuppusamy P
  • 9.
    Creating Method -Example 4. Method with return-type with parameters public class FactEx3 { public int Fact (int n) // Method Definition { int f =1; for(int i=1;i<=n;i++) f=f * i; return f; } // Method calling public static void main(String args[]) { Scanner sin=new Scanner(System.in); int inp = sin.nextInt(); int factorial = Fact(inp); System.out.println(factorial); } } Dr. Kuppusamy P
  • 10.
    Creating Method -Example 5. Static methods public class StaticEx { public static int Fact (int n) //method definition { int f=1; for(int i=1;i<=n;i++) f=f*i; return f; } public static void main(String[] args) { Scanner sin=new Scanner(System.in); int num = sin.nextInt(); int factorial = Fact(num); //method calling System.out.println(factorial); } } Dr. Kuppusamy P
  • 11.
    Creating Method -Example 6. Non-static methods public class NonStaticEx { public int Fact (int n) //method definition { int f=1; for(int i=1;i<=n;i++) f=f*i; return f; } public static void main(String[] args) { Scanner sin=new Scanner(System.in); int num = sin.nextInt(); NonStaticEx m = new NonStaticEx(); //object or instance creation for the class NonStaticEx int factorial = m.Fact(num); //method calling System.out.println(factorial); } } Dr. Kuppusamy P
  • 12.
    Passing arrays asParameters Dr. Kuppusamy P
  • 13.
    Passing arrays asParameters • Pass arrays to a method like normal variables. • When pass an array as an argument, actually the address of the array in the memory is passed (reference). Creating Method with Arrays i. Without return an array modifier returnType Methodname (data-type array-name[]) { // method body } ii. With return an array modifier data-type[] Methodname (data-type array-name[]) { // method body } Dr. Kuppusamy P
  • 14.
    Example import java.util.*; public classArrayPass { public int min(int [] array) { int min = array[0]; for(int i=0; i<array.length; i++) { if(array[i] < min) { min = array[i]; } } return min ; } Dr. Kuppusamy P
  • 15.
    Example public static int[]descending(int [] array) { for (int i = 0; i < array.length; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } return array ; } Dr. Kuppusamy P
  • 16.
    Example public static voidmain(String args[]) { Scanner sc = new Scanner(System.in); int size = sc.nextInt(); int[] myArray = new int[size]; for(int i=0; i<size;i++) { myArray [i] = sc.nextInt(); } ArrayPass m=new ArrayPass (); int max= m.min(myArray); System.out.println(max); int dsc[] = m.descending(myArray); for(int i:dsc) System.out.println(i); } } Dr. Kuppusamy P
  • 17.
    Method Overloading injava Dr. Kuppusamy P
  • 18.
    Method Overloading injava • Method Overloading is a class contains more than one method with the same method name. But varying the argument lists like count, data type and arguments sequence. • Example: • Addition of two number for integer as well as float. • Advantages • Method overloading increases the readability of the program. Different approaches of method overloading 1. Number of parameters. • add(int a, int b) • add(int a, int b, int c) 2. Data type of parameters. • add(int a, int b) • add(int a, float b) 3. Sequence of Data type of parameters. • add(int, float) • add(float, int) Dr. Kuppusamy P
  • 19.
    Method Overloading injava import java.util.Scanner; public class Overload { public static int add(int a, int b) { return a+b; } public static float add(float a, float b) { return a+b; } public static void add(float a, int b) { System.out.println(a+b); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); int isum=add(num1,num2); float fsum=add(34.34f,34.34f); add(10.0f, 20); System.out.println(fsum); } } Dr. Kuppusamy P
  • 20.
  • 21.
    Recursion in java •Recursion is a process in which a method calls itself continuously. • A method in java that calls itself is called recursive method. • It makes the reusability of code. So, reduces the program size. • Syntax returntype methodname() { statements; methodname(); } Dr. Kuppusamy P
  • 22.
    Factorial using recursionin java • Example import java.util.*; public class RecFact { public static int fact(int n) { if (n <= 1) { System.out.println(n); return 1; } else { System.out.print( n+ " * "); return n * fact(n-1); } } public static void main(String[] args) { int n; System.out.println("Enter the no to find factorial " ); Scanner sin = new Scanner(System.in); n = sin.nextInt(); int res = fact(n); System.out.println("Factorial " + res); } } Dr. Kuppusamy P
  • 23.
    Fibonacci using Recursionin java • Example import java.util.*; public class FibRec{ public static int fib( int n) { if(n == 0) { return 0; } if(n==1 || n==2) { return 1; } else return fib(n-2) + fib(n-1); } public static void main(String [] arg) { int n; Scanner sin = new Scanner(System.in); System.out.println("Enter the fib series"); n = sin.nextInt(); for(int i=0; i<=n; i++) { System.out.print(fib(i)+ " "); } } } Dr. Kuppusamy P
  • 24.
    References Dr. Kuppusamy P HerbertSchildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition, 2017.