return_type method_name(parameter-list)
return-type defines the value that method can return a the end of its execution.Method_name is the name of method that depends on you and parameter-list is the list of arguments that you need to use in your code.Note one thing that return_type and parameter_list should have same data type.The word void means nothing.If function have void return type then we can say it returns nothing.Depending upon return_type and parameter_list we can divide it in four parts.Here we have divided it in three
parts mainly .
Methods in java
1.Method with no return type and no arguments
void name(void);
If you wish do additional idea you want it then you’ll execute below simple example.
Source code:–
class rectangle{
int length;
int width;
void calarea()
{
System.out.println(“Area of rectangle is :” +(length*width));
}
}
class method1{
public static void main(String args[])
{
r.width=20;
r.calarea();
}
}
output:–
Area of rectangle is:200
2.Method with return sort
As the name suggest,it have return type but does not have parameters.So after we use it in main class we have to store the returning value first of all.
syntax
return_type name(void)
Simple example is there below..
source code
class sum{
int a;
int b;
int cal()
{
return a+b;
}
}
class method2{
public static void main(String args[]){
int c;
sum s=new sum();
s.a=10;
s.b=20;
c=s.cal();
System.out.println(“The value of sum = “+c);
}
}
output:
The value of sum =30
3.Method with return type and arguments list
It contains both return type as well as parameter list.So we have to pass the parameters in function body so have to store its returning value mainly.
Syntax
return_type name(arguments)
Source code
import java.util.Scanner;
class sq{
int a;
int cal(int a)
{
return a*a;
}
}
class method3{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println(“Enter number”);
sq e=new sq();
int k=s.nextInt();
int d;
d=e.cal(k);
System.out.println(“Square is =”+d);
}
}
output:
Enter number
4
Square is 16
Logic of all above code:In first simple program we have initialised rectangle’s variables with using object in main class and then with help of that object we called the method within the other class.In second example we have defined the return type integer and initialise both value and stored inside other variable of its sum.In third example we passed the arguments to the function of other class and calculated square using that value.If you find any difficulty(I know its not possible) then comment below and don’t forget to like our fb page.Happy coding!!