0
Return statement
Hey, what is retutn statement exactly used for? Does it print a line, does it save a value that we can use later on, or does it give the value of the method?
5 Answers
+ 4
They stop the execution of a function and return a value (as the name already suggests). In JS, if the return statement is not present, then the function would return undefined. For example:
function square(a) {
return a * a;
}
console.log(square(2));
// returns 4
+ 3
return returns a value to the line calling the function the return statement is in.
In Python for example,
def hello():
return "hi"
print(hello())
outputs hi to the screen.
Alternatively,
def hello():
print("hi")
hello()
does the same thing.
+ 2
Try reading through this bit of the course again
https://www.sololearn.com/learn/Java/2153/
You use the returned value by storing it in a variable.
0
So, it is only used to stop the flow? I still didn't get much. How do we use the returned value in java in another method?