-2

I feel somehow I don't understand point of using "return" value, so I will give you two examples and maybe someone will clear that up for me. What is the difference between this method:

int giveSecret(){
return 42;
}
//code in between
int theSecret = life.giveSecret();
System.out.println(theSecret);

(my expected console output): 42

and this method:

void giveSecret(){
//code that "resets" variable to value 42
}
//code in between
int theSecret = life.giveSecret();
System.out.println(theSecret);

(my expected console output): 42

Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
Krushe
  • 23
  • 8
  • 2
    The second snippet doesn't pass compilation since `life.giveSecret()` returns nothing. – Eran Jul 26 '17 at 11:15
  • Why do you think there is always a variable that can be manipulated in a method like `giveSecret`? – Tom Jul 26 '17 at 11:16
  • You should try this first. You'll see that int theSecret = life.giveSecret() won't compile since int != void. – Pieter De Bie Jul 26 '17 at 11:18
  • `//code that "resets" variable to value 42`... what variable? And as pointed out, if you have `void giveSecret()` then attempt `int theSecret = life.giveSecret();` then that's an error since `giveSecret()` return is declared as `void` but you're attempting to assign it as if it had an `int` return value. You need a `return` statement (with argument whose type matches what you declared the return value to be) to tell the interpreter/compiler to return a value to the caller. Without it, nothing will be returned. The question, *is it in complexity or?* is very unclear. – lurker Jul 26 '17 at 11:21
  • Keep in mind, there's much more to returning a value than just returning a predefined constant. Usually the value being returned was calculated for the specific input given to the function. Functions represent a conversion or use of data; the `return` returning the resulting data. Your over simplified examples miss the point. – Carcigenicate Jul 26 '17 at 11:22
  • @Eran Imagine that I've created some class and "life" is object of that class, and imagine giveSecret method is programmed in such a way that it just changes every integer variable to value 42 when you call it. Imagine in the "code between" the value of theSecret is some random integer, and I want to reset that value to 42 after a while. – Krushe Jul 26 '17 at 11:48
  • @Carcigenicate I'm at the beginning stage of learning Java, trying to give sense everything I stumble upon. :S Maybe in later chapters I will find sense of "return". – Krushe Jul 26 '17 at 11:54
  • @Krushe Read up on the use of functions. Once you understand why functions are useful, the use of `return` becomes obvious. Understanding how to properly use functions is one of the most important things you can learn Imo. Right up there with learning how to use a debugger. – Carcigenicate Jul 26 '17 at 11:56
  • @Carcigenicate What is the difference between method and function? (Java) Can you provide me with learning sources for functions in Java? Thanks! – Krushe Jul 26 '17 at 12:02
  • @Krushe A method *is a* function that has access to an object's internals. Sec, I'll post a link to a question that I asked years ago. – Carcigenicate Jul 26 '17 at 12:03
  • @Krushe Not your question exactly, but worth a read over: https://stackoverflow.com/q/30402169/3000206 – Carcigenicate Jul 26 '17 at 12:04
  • @Carcigenicate Thank you! So, in global I can "give" something to method (pass it an argument) and based on that argument method will do something, or I can just call method to "do what is coded in it" or I can return ("extract") any data from method I need in order to do "this and that"? – Krushe Jul 26 '17 at 12:18
  • @Krushe Basically, but *always* prefer returning a value to don't stuff in the background when possible. It's difficult to explain why until you do more complicated programs, but modifying globals *always* makes things more difficult later when abused. If you need an example, think of the plus function, `+`. You give it numbers as arguments, and it `return`s the sum of the numbers. Imagine if it modified a global instead of returning. It would become much more difficult to use. – Carcigenicate Jul 26 '17 at 12:26
  • @Carcigenicate What I can conclude In the case of return, when you call method with return value at the same time method will do the sum and return final value directly to caller and "wont save" final result at some global variable in method named ex. "sum" rather than having such "non-return" method where in order to return value from it you have to write two lines: first to call method, second to store whatever is in "sum" variable is? Would you mind writing simple code for + when it modifies a global instead of returning and one with returning if I'm wrong with my conclusion? Thanks! – Krushe Jul 26 '17 at 12:59
  • @Krushe My shift just started. It will have to wait until I'm home, and then I have to pack for a trip. I'll try to later. – Carcigenicate Jul 26 '17 at 13:00
  • @Carcigenicate No problem, whenever you find some time it will be appreciated! – Krushe Jul 26 '17 at 13:12
  • @Carcigenicate I've just realized that answer bellow explained what you intended haha – Krushe Jul 26 '17 at 13:19

1 Answers1

1

Sure, languages could work like your latter example. Every function could have a global variable associated with it where the "result" is stored. You would call the function then read the result from the global variable. However:

  1. It's disgusting. Why need to have another global name? Why have to somehow know what variable is associated with which function?

  2. It's not thread-safe. Multiple threads might call the function at the same time. Now, which thread's result is present in the global variable?

Language designers figured this stuff out long ago, and it has stuck.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Thanks thats constructive! I'm at beginning stage of learning Java, and I stumbled upon this not-so-well-explained-yet example and tried to give it a sense. Hope in later chapters I will fully understand point of "return". – Krushe Jul 26 '17 at 11:57