Parameter Passing And Returning A Value From A Method In Java

Hello everyone!! Today we will learn parameter passing and returning a value from a method in java.

In previous post we learnt how to declare a method, what is a method and why it’s used in java. Now here we will see some more properties of method.

Here i will cover two topics:

  • passing parameters to the method
  • returning values from a method

Parameter Passing And Returning A Value From A Method In Java: passing parameters to the method

Let me explain passing parameters to the method with an example,

public class String_Length {

public static void main(String[] args) {
 
first_Method();

}
 
public static void first_Method()
{
System.out.println("Hello Flower Brackets!!");
}

}

In the above example, first_Method() is simple to execute and print. It doesn’t solve much of the purpose because every time it’s only printing “Hello Flower Brackets!!”. Now to make it more flexible we can add an argument to first_Method() function. The declaration remains same.

Also Read – Learn Inheritance In Java

For example, say, i want to greet some person based upon their names. So we can pass an argument to first_Method() function like this,

public class String_Length {

public static void main(String[] args) {
 
first_Method("George");

}
 
public static void first_Method(String full_Name)
{
System.out.println("Hello " + full_Name);
}

}

Output:

Parameter Passing And Returning A Value From A Method In Java

We will pass the name in the form of string variable. So this kind of declaration inside round bracket in the function first_Method is called the parameter passing and this parameter name which is a string is used inside the function. So if you want to say hello or whatever name is passed to the function then it will print that name. So whenever we call first_Method() function to the main method, for example we call the name “George” then it will greet “Hello George”.

first_Method() can be used to greet more than one function like this,

public class String_Length {

public static void main(String[] args) {
 
first_Method("George");
first_Method("Sam");
first_Method("Steve");

}
 
public static void first_Method(String full_Name)
{
System.out.println("Hello " + full_Name);
}

}

Output:

Parameter Passing And Returning A Value From A Method In Java

So this makes first_Method() function a little bit flexible in a way that we can use it according to our use. The other use of first_Method() function is we can use it to add with an integer, for example, we can pass any number of parameters. It’s not necessary that you need to pass only one parameter. You can pass multiple parameters like this,

public class String_Length {

public static void main(String[] args) {
 
Sum(2,3);
Sum(5,5);
Sum(10,4);
 
}
 
public static void Sum(int x,int y)
{
System.out.println(x + y);
}

}

Output:

5
10
14

As you can see in the above example we have declared Sum() function which adds two values. In here we can pass multiple values namely int x and int y. Then i need to call Sum() function and the output is as shown above.

You can add some more integers with same function as shown above. It depends on you how many parameters you want to pass to your function. The only thing is you just need to give the datatype and the variable which will hold these values. Indirectly the value of “x” becomes 2 and value of “y” becomes 3. So they will be replaced and they will be called and the answer will be printed on the console. This makes your code really easy. Using a same function you can add different values. It makes the code readable and understandable.

This is called parameter passing. Here we can only pass the values and if we want to access the result using parameter passing we cannot access the results. Using Sum() function it’s not possible. But there is a way to do this and the way to do this is by returning some value. This topic is covered in the next section.


Parameter Passing And Returning A Value From A Method In Java: returning values from a method

So we will modify Sum() function a little. Right now it’s returning void. Void means it is not returning anything. But when we want to use it inside our main function we can return integer value. And this return value will tell what kind of value you want to return from your function and the same you will return. Let’s see an example,

public class String_Length {

public static void main(String[] args) 
{
int total = Sum(1,3,1);
System.out.println(total);
}
 
public static int Sum(int x,int y,int z)
{ 
return (x + y + z);
}

}

As you can see in the above example, i have written,

return (x + y + z);

This way what you are basically doing is you are taking all the parameter values in Sum() function and you are adding (x + y + z) and you are returning from Sum() function. Here in the above example we are just passing x, y and z values and we are returning. And whenever a function return anything you can store it in other variable but of the same datatype. So the datatype of returning value and the value in which you want to store the returned value should be same like this,

int total = Sum(1,3,1);

what this basically do is x will replace 1, y will replace 3 and 1 will replace z. Then all the values are added and it will return sum and stored in total variable. The value is printed using below statement,

System.out.println(total);
conclusion:

So in this way the function or methods made really easy to organize your code and make it really concise, readable, understandable and it also makes your code prettier. In the same way you can make any logic using above functions. For example you can make a multiplication function to make multiplication you can make a function to do divisions, add two numbers, subtract two numbers or any complicated arithmetic or logical or anything you want to do you can do it inside a function and you can take arguments or parameters and return the results so that you can use it in your main method. So i hope you have understood the basic concept of passing parameters and returning values like above.

About the author

admin

View all posts

Leave a Reply

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