+ 5

Is a method with a void statement like a piggy bank?

I'm new here and I'm asking myself if it's true to see a method with a void statement like a piggy bank. Like you can put some arguments on it but never take the arguments back. Once you put something on it you can't take it back because it doesn't return. I'm right thinking like that?

23rd Aug 2017, 9:12 PM
LouLou
LouLou - avatar
7 Answers
+ 9
You can still alter the value of non-local variables by passing parameters via reference though. The piggy-bank analogy isn't exactly inaccurate, can't think of a better one at the moment.
24th Aug 2017, 1:26 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Not true. in java, I use many void methods/functions, which change the value of global/inherited variables which are then used to update my gui with that variable in a sperate method/function
28th Aug 2017, 3:13 AM
DeleteThisAccount
+ 3
You can take things back from piggy banks though... so... yeah... :D Anyways, void means that the function isn't going to return anything back to the function that called it. So it'll do whatever code is in the function that's being called, and simply return back to you. It makes it really easy to get values back though. However, who knows what the other function did, because there are many ways to get data back from whatever the function did.
23rd Aug 2017, 10:35 PM
AgentSmith
+ 2
void means that the method does not return a value. If the method specifies any other return type, the method will return a value of that return type. This can be either a primitive value (like int or boolean) or an object (like String, Number, Date, .. whatever). so it IS like a piggy bank in that you put something in and don't get anything back. but it IS NOT like a piggy bank in that you don't store anything in the method. The method may just do something, like print out a line, but may also store data somewhere else. it just doesn't return any value of that date or whether it was successful or anything. (more like sending am untraced letter and not knowing if it was receiver or not). if it does store data, you could retrieve that date with another method; that method *would* have a return type, of whatever type it returns.
24th Aug 2017, 11:47 AM
marit vandijk
+ 2
It does not return, but you can print output using it. Like your analogy of piggybank...
26th Aug 2017, 5:37 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 1
I think the "piggy bank" thought is correct BUT this piggy bank can do something if you give it what it needs!!! Ok, jokes apart😅 Functions which are void can do other things like printing a statement, call another function,etc.
28th Aug 2017, 3:38 PM
Gammaburst
Gammaburst - avatar
0
You can't only get values from return type. You can also pass arguments as a reference, throw a value (Yes, what you throw must not always be an exception.) or simply use global variables. And I probably even forgot something.
27th Aug 2017, 9:56 AM
Timon Paßlick