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);