Question:
What does the "return" statement do in Java?
y0wzahman123
2011-09-11 00:42:13 UTC
I just started Java and I came across the return statement, but I don't know what it does. What exactly does the return statement do?Does it add 1 to the variable, then return the sum to the main class? I know the output of the following program is 232, but why? It'd help if someone explained to me in a step-by-step explanation or made a sample program with comments.

public class D {
public static int p(int n) {
System.out.print(n);
return n+1;
}
public static void q(int n){
System.out.print(n-1);

public static void main(String[] args) {

int x = p(2);
System.out.print(x);
q(3);

}
}
Seven answers:
?
2011-09-11 00:53:14 UTC
return ends that method and sends whatever is after it back to the statement that called it (or if the method returns a void, it doesnt send back anything. That is usually used for print statements). So if I had a method p that accepts an integer and then returns it,, i could put int x = p(5); and x = 5. You are not restricted to numbers for return values. You can return strings as well



In this case, x is an the integer that function p(2) returns. p(2) says run method p with input of 2. Method p take in an integer, prints that integer, then sets x = to whatever that integer is + 1. So in this one, it will print 2, then set x = 3, then print 3, then method q accepts 3 (from q(3)) and prints 3-1 which is 2
galarneau
2016-09-29 03:42:33 UTC
Return Statement In Java
Letisha
2015-08-12 02:57:17 UTC
This Site Might Help You.



RE:

What does the "return" statement do in Java?

I just started Java and I came across the return statement, but I don't know what it does. What exactly does the return statement do?Does it add 1 to the variable, then return the sum to the main class? I know the output of the following program is 232, but why? It'd help if someone...
green meklar
2011-09-11 07:50:11 UTC
It exits the method and returns control to the caller. If the method is non-void, it also returns a value which the caller can then use.



In this case, the call to p(2) will return a value of 3, because the value returned is always the integer sum of the argument and 1. The local variable x, inside main(), will then be assigned this value 3.
CP
2011-09-11 00:57:12 UTC
A return statement returns a value to the method that calls it.



for p, it prints the number you send it, which is stored in variable n:

System.out.print(n);

this is where your first 2 comes from.



method p then adds one to n, n+1, so n now has 3 in it, and returns that 3 to the main method in variable x.

x is then printed, so that is where the 3 in 232 comes from.



method q is now sent 3, which is stored in n, and is printed as n-1 which equals 2, so this is your last 2.



You would get the same result if you had called q with x, q(x); because x had the value 3 as well.
anonymous
2011-09-11 01:03:45 UTC
On the line where it reads "int x = p(2)", this is calling the method "p" and passing in an integer of 2. The result of that method will be inserted into the variable "x".



So in the method "p", it is printing the integer that was passed in (an integer called "n"), and then "returning" (hence the "return" statement) an integer that equals n + 1. Since 2 was passed in, it would print "2" and then it would return the value of 3 (which is 2 + 1) back to where the "p" method was called. In this case, "p" was called in the line that reads "int x = p(2)" so the value 3 is given to the variable "x".



The next line after that prints out the variable we just assigned, so it would print the value of 3.



The "q" method just takes the value it is passed (which is 3) and prints out the value minus 1 (which is 2).



So overall, the program will print 232.



Hope that helps!
Anza Power
2011-09-11 01:09:09 UTC
The return statement tells the function what value to give back, for example take the math functions, there's pow(x,n) which gives you x to the power of n, abs(x) gives you the absolute value of x, sin(x) gives you the sinus of x...etc



And you use them like this:

y=pow(6,2);

z=abs(-5 + 2 * y);

p = sin(z+1) + sin(3.14);



Now what happens is that the values that you give to the function between the parenthesis (those are called arguments) are COPIED into the arguments you define in the funcion, so for example if you have the function:



public static void myFunc(int x){

......//do the stuff with x;

}



public static void main(String[]args){

......int z=10;

......myFunc(z);

............//This is like writing, ......int x=z;

..................................................//do the stuff with x;

......myFunc(z+20-14 / 2);

............//This is like writing, ......int x=z+20-14 / 2;

..................................................//do the stuff with x;

}



So you see even when we wrote myFunc(z), z got COPIED into x, so from now on any change we do to x (in the function) WILL NOT apply to z.



Now as for the return function, imagine if we wanted to make a mathematical function like the ones above, so we can use it in statements like:



int a = myFunc(17) + myFunc(y+1);



What you do is that in the function declaration, in the "public static void funcName()" part, instead of void you choose the type of the variable you want your function to behave like, and then inside your function do all the calculations and then at the end of the function return the result



For excample if you want a functionthat will sum all the numbers from 1 to n, you write:



public static int sumNums(int n){

......int sum = 0;

......for(int i=1;i<=n;i++){

............sum += i;

......}

......return sum;

}



And then you use it like this:

int s = sum(50);

System.out.println("The sum of numbers from 1 to 50 is "+s);


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...