Void is a valueless state, so why is it used? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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.

28th Dec 2016, 5:54 PM
Hakeem Thomas
Hakeem Thomas - avatar
3 ответов
+ 6
Void returns nothing (that's why it's called "void")
28th Dec 2016, 8:13 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 2
always there is nothing...so here it was named "void"
28th Dec 2016, 9:47 PM
Ahmad S Kanaan
Ahmad S Kanaan - avatar
+ 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)
29th Dec 2016, 4:09 AM
Leon Nascimento
Leon Nascimento - avatar