0
Void is a valueless state, so why is it used?
I know void prints something. I just don't understand what it does. Please give an example with explanation. I'm a visual learner.
3 odpowiedzi
+ 6
Void returns nothing (that's why it's called "void")
+ 2
always there is nothing...so here it was named "void"
+ 1
Void doesn't necessarily prints anything.
There will be functions where you'll need to return something (be it an int, String or even your custom data type), but there are functions that simply change states or updates variables without necessarily returning anything.
If you had a class Car which had the attribute speed and a method named brake(), Car.brake() would change speed, but it wouldn't need to return the new speed value. You could have a method getSpeed() that could do that for you.
What is the advantage? You can modularize your object better with a function that does one thing (break) than a function that does more than one thing(break and return speed)