What is Method?
- A Method is a block of code that performs a specific task.
- Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.
- A subprogram is also called as a Method.
- A “C” Program is collection of one or more Method.
- A “C” Program is at least one Method called main() Method.
How to use Method?
A Method is a block of statements that is used to solve a specific task or need to perform a same task more than once. In such case you have two options -
a) Use the same set of statements every time you want to perform the task
b) Create a Method to perform that task, and just call it every time you need to perform that task.
Here, we use option (b) for the practices and a good programmer always uses Methods while writing codes in C.
Need of Method?
Methods are used because of following reasons –
a) To improve the readability of code.
b) Improves the re usability of the code, same Method can be used in any program rather than writing the same code from scratch.
c) Debugging of the code would be easier if you use Methods, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by Method calls.
Library Method / System Defined Method
The standard library methods are built-in methods in “Java”.
These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE.
Syntax :
Call a Method : Method name();
For Ex.
print() is a method of java.io.PrintSteam. The print("...") prints the string inside quotation marks.
sqrt() is a method of Math class. It returns square root of a number.
Step to User Defined Method
Class Modifier
Declaration of Method
Definition of Method
Call Method
Definition of User Defined Method
Class Modifier Return Type Method Name (Argument List)
{
Statement 1
Statement 2
……………
Statement n
}
Example:
public void abc (int a, int b)
{
int c;
c=a+b;
System.out.println(“The sum is =%d”,c);
}
Call of User Defined Method
object name.Method Name();
Or
Object name.Method Name(Argument List);
Example:
obj.abc();
Or
obj.abc(56,89);
1. Method without Argument without Return Type
In this type of Methods there is no data transfer between calling Method and called Method.
Simply the execution control jumps from calling-Method to called Method and executes called Method, and finally comes back to the calling Method.
Example Method without Argument without Return Type
class Main {
public static void main(String[] args) {
Output obj = new Output();
System.out.println("About to encounter a method.") // calling myMethod() of Output class
obj.myMethod(); // Calling Method
System.out.println("Method was executed successfully!");
}
}
class Output {
// public: this method can be called from outside the class
public void myMethod() // Called Method
{
int a,b,c;
a=23,b=23;
c=a+b;
System.out.println(“The Sum is =“ +c);
}
}
2. Method with Argument without Return Type
In this type of Methods there is data transfer from calling-Method to called Method (parameters)
but there is no data transfer from called Method to calling-Method (return value).
The execution control jumps from calling-Method to called Method along with the parameters and executes called Method, and finally comes back to the calling Method.
Example Method with Argument without Return Type
class Main {
public static void main(String[] args) {
Output obj = new Output();
System.out.println("About to encounter a method."); // calling myMethod() of Output class
obj.myMethod(23.23); // Calling Method
System.out.println("Method was executed successfully!");
}
}
class Output {
// public: this method can be called from outside the class
public void myMethod(int a, int b) { // Called Method
int c;
c=a+b;
System.out.println(“The Sum is=“ +c);
}
}
3. Method without Argument with Return Type
In this type of Methods there is no data transfer from calling-Method to called-Method (parameters) but there is data transfer from called Method to calling-Method (return value).
The execution control jumps from calling-Method to called Method and executes called Method, and finally comes back to the calling Method along with a return value.
Example Method without Argument with Return Type
class Main {
public static void main(String[] args) {
int result;
Output obj = new Output();
System.out.println("About to encounter a method.");
// calling myMethod() of Output class
result=obj.myMethod(); //calling method
System.out.println(“The Sum is=” +result);
System.out.println("Method was executed successfully!");
}
}
class Output {
// public: this method can be called from outside the class
public int myMethod() { // called Method
int a,b,c;
a=23;b=23;
c=a+b;
return c;
}
}
4. Method with Argument with Return Type
In this type of Methods there is data transfer from calling-Method to called-Method (parameters) and also from called Method to calling-Method (return value).
The execution control jumps from calling-Method to called Method along with parameters and executes called Method, and finally comes back to the calling Method along with a return value.
Example Method with Argument with Return Type
class Main {
public static void main(String[] args) {
int result;
Output obj = new Output();
System.out.println("About to encounter a method."); // calling myMethod() of Output class
result=obj.myMethod(23,23); //Calling Method
System.out.println(“The Sum is=” +result);
System.out.println("Method was executed successfully!");
}
}
class Output {
// public: this method can be called from outside the class
public int myMethod( 23,23) {
int c;
c=a+b;
return c;
}
}
Advantages of Method
- Programs can be divided into small and simple tasks.
- Allowing the code to be called many times.
- Easier to error Handling.
- Easier to read and update.
- Code Re usability.
Conclusion of Method
- To avoid repetition of code and bulky programs Methodically related statement are isolated into a Method.
- Method definition define the body of Method.
- Method deceleration specify the return type of the Method and the type of parameters its accepts .
- It is used by the one or more times.
- It is easy to perform by given steps.