Return statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

21st Jul 2018, 8:04 AM
Icefield
Icefield - avatar
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
21st Jul 2018, 8:19 AM
Harsh
Harsh - avatar
+ 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.
21st Jul 2018, 8:15 AM
Satyam
+ 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.
21st Jul 2018, 12:24 PM
Duncan
Duncan - avatar
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?
21st Jul 2018, 8:25 AM
Icefield
Icefield - avatar