Header Ads

What Is Return Statement In Java??

image credit:www.python-course.eu
In my previous post here I have shown how to send arguments and how a method can receive parameters. In this post we will look the return statement for java.

Methods can send some value to the calling method. Now how JVM will understand that the calling function is expecting some value??

Well if JVM encounters a "return" statement it will understand that the calling function is expecting something and the called method is suppose to send something.

int a=10;
int b=20;
int val=add(a,b)
//called the function named as add with two arguments
public int add(int x,int y)
//called method add will accept two parameters
return (x+y);
//this will return addition of two variables.


There might be a case where the called method is of type void. here the return keyword is used only to exit the method and it is not mandate to give return statement here.

But when we have a proper return statement for a method we also need to take care about the type of the return.

Now if we want to send more than one value from the called method then what will be our approach??

Well ...for that case we need to create an array and will return the array to the calling method.

Powered by Blogger.