Lecture 5
Lecture 5
Methods (functions)
• In this lecture you will learn how to create a method, how to call a
method and function overloading.
2
Method Declaration
• Before using a method, we must create/declare it.
3
Method Declaration
• Method Signature: Every method has a method signature. It is a part of
the method declaration. It includes the method name and parameter list.
4
Method Declaration
• Return Type: Return type is a data type that the method returns. It may
have a primitive data type, object, collection, void, etc. If the method does
not return anything, we use void keyword.
5
Naming a Method
• While defining a method, if possible the method name must be a
verb and start with a lowercase letter. If the method name has
more than two words, the first name must be a verb followed by
adjective or noun. In the multi-word method name, the first letter of
each word must be in uppercase except the first word. For
example:
6
Types of Method
• Methods are also known as Procedures or Functions:
– Procedures: They don't return any value.
– Functions: They return value.
– User-defined Method
• The method written by the user or programmer is known as a user-defined
method. These methods are modified according to the requirement.
7
How to Create a User-defined Method
• Let's create a user defined method that checks the number is even
or odd. First, we will define the method.
9
How to Call or Invoke a User-defined Method
import java.util.Scanner;
• Once we have defined a method, it should be
public class EvenOdd
called. The calling of a method in a program is
{ simple. When we call or invoke a user-defined
public static void main (String args[]) method, the program control transfer to the
{ called method.
//creating Scanner class object
Scanner scan=new Scanner(System.in); • In the code snippet, as soon as the compiler
System.out.print("Enter the number: "); reaches at line findEvenOdd(num), the control
transfer to the method and gives the output
//reading value from the user
accordingly.
int num=scan.nextInt();
//method calling
• The void keyword allows us to create methods
findEvenOdd(num); which do not return a value. This method is a
} void method which does not return any value.
public static void findEvenOdd(int num)
{ • Call to a void method must be a statement i.e.
//method body findEvenOdd(num); . It is a Java statement which
if(num%2==0) ends with a semicolon as shown below.
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
10
}
Calling an function returning int data type
public class Addition
{
public static void main(String[] args)
{
int a = 19;
int b = 5;
//method calling
int c = add(a, b); //a and b are actual parameters
System.out.println("The sum of a and b is= " + c);
}
//user defined method
public static int add(int n1, int n2) //n1 and n2 are formal parameters
{
int s;
s=n1+n2;
return s; //returning the sum
}
}
11
Calling an function
• Note:
– In the above program in the function add, there are two parameters of
data type int
– The function will return a value of data type int
12
Calling an function returning double data type
public static void main(String[] args) {
double c = 7.3;
double d = 9.4;
double result2 = minFunction(c, d);
System.out.println("Minimum Value = " + result2);
}
// for double
public static double minFunction(double n1, double n2) {
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
13
void keyword
• The void keyword allows to create methods which do not return a
value. In the following example we're considering a void method
displayMarks. This method is a void method which does not return
any value. Call to a void method must be a statement i.e.
displayMarks(82);. It is a Java statement which ends with a
semicolon as shown below.
• Example:
• The following program shows an example of passing parameter by value.
The values of the arguments remain the same even after the method
invocation.
15
Example of Passing Parameters by Value
public class swappingExample {
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
} 16
}
Example of Passing Parameters by Value
• Output
17