Basic Java – 4 || More on methods(Return Type and Parameters)

          There are two main key functions for methods. Parameters and Return Type. If we talk about Parameters, these are the inputs for the methods. On the basis of these inputs methods performs any action. Once all the steps get executed mentioned within the block of a method, the output of that method will get returned to the place from where the method has been called in the code. This output can be sent to its native place using return type for that method.      

Methods

                       Return_type method_Name(param1, param2,)

                              int                  sum       ( int a   ,  int b)        

          In the above diagram, there is a method int sum(int a,int b) with name sum, which takes two input parameters of integer type(int a,int b). Also, after execution, it will return an integer value int sum to the Test.Class using return keyword. This returned value can be stored in a local variable int total, which can be used further in the program.

Related Links:

OOPs Concept:

Java Question And Answer:

Key Factors while declaring Methods

    • Return_type: data type of the value which will be returned from a method after execution completed.
    • Method_name: User-defined any logical name to identify method functionality. As per standard, it should start with lowercase, rest words should have the first letter in upper case.
    • Param1, param2: parameters have been divided into two parts data type and variable name. We can define multiple parameters using comma separator. (More on data types and Variables)
    • return: return is an inbuilt keyword which can return only one value to its original place.
    • A method may have zero or more parameters: If a method has no parameters the output of the method will always remain same.
    • A method can have only one return type: A method can only return only one value using return keyword.
    • A method can have void return type: If a method is declared void return type than output cannot be returned to its original place.
    • Multiple methods with the same name: A class can have multiple methods with same name and same parameters. This is called Polymorphism concept in OOPs. The process is called Method Overloading and Method Overriding.

Example Code-Methods:

public class SampleExample 
{
	public static int sum(int a, int b)
	{
		int c=a+b;
		return(c);
	}
	
	public static void main(String[] args) 
	{
		int total;
		total=sum(10,20);
		
		System.out.println("Total = "+total);
	}
}

1349total visits,2visits today

Leave a Reply

Your email address will not be published. Required fields are marked *