+ 1
How return works?
Recently studying java. So i've seen a lot of return value lately in the coding tutorials and i'd say the return doesn't make sense to me on how it woks. So can someone explain to me how return works?
5 Antworten
+ 4
Methods and return types will be explained in lesson 28 and 29 of the sololearn Java course, keep learning.
👉On Q&A only tag the relevant programming language
+ 2
Return makes your method a data type.
int Sum (int x, int y)
int result = x + y;
return result;
+ 2
An example of the fastest algorithm for performing operations on 2 is the use of return. Returning to the times we need to give us the result value after performing an operation on a function is very useful
Examples:
char ch = getchar(); // getchar return inputed char
int power=pow(1,2); // return 1^2 -cmath library
+ 1
My noob understanding is that return does two things:
1. It stops a function, acting kind of like a break; does to a loop. That function code thread gets finished, the variables that were created in that function scope get forgotten/released, and
2. if the return has a value type (like int or char) it gets sent back to the main/calling function. To use the returned value you could use a variable assignment to call the function eg: x = myFunc(y,z);
Many functions that don't send back a result instead return 0; that is often used to signify that there wasn't an error during that execution.
I've not got error checking/catching in my list of competencies yet... So there is still much more for me to learn!