Return statement | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren

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 - avatar
5 Antworten
+ 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 - 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
+ 2
Try reading through this bit of the course again You use the returned value by storing it in a variable.
21st Jul 2018, 12:24 PM
Duncan - avatar
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 - avatar