Whats the point in making a method that returns nothing (void)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Whats the point in making a method that returns nothing (void)?

12th Apr 2017, 12:53 PM
Green
12 Answers
+ 3
String name; public void setName (String name ){ this.name=name; }
12th Apr 2017, 7:35 PM
Haileyesus Shitalem
Haileyesus Shitalem - avatar
+ 2
I stand corrected, Haileyesus is right. In C++ you always return an integer. So: - Java > Always void - C++ > Always int - C# > Can do both
22nd Apr 2017, 3:40 AM
Kurt
Kurt - avatar
+ 1
I think that's to keep clean your code, run faster the code cause it knows nothing is expected at the end and it you will get an error if you try to get the value of void function. Don't know about Java but in my memory in Action Script it was like that.
12th Apr 2017, 12:57 PM
Geoffrey L
Geoffrey L - avatar
+ 1
A method without a return (void) is basically a procedure. You run a block of code without returning a value, unlike a function which does.
12th Apr 2017, 11:17 PM
Hassie
Hassie - avatar
+ 1
Because you don't always need a value returned. For example: When you ask a user for his age, you send the value to your class, no need to return that same value back to your main. Would it be a issue if it returned the same value? No. But because of the 'void' keyword, the compiler knows not to return the value, and skips that procedure, thus making your program work faster and with less memory usage.
13th Apr 2017, 8:22 PM
Kurt
Kurt - avatar
+ 1
Edit: Answered for C# Edit 2: Corrected the C++ statement thanks to Haileyesus, C++ will always return an integer. For Java: In Java your main can only return void. If you try to return anything else, you will get a compile error. Communication with other software need to be done with code. For C#: @mikiyas & & @haileyesus Main can return void or integer, but it depends on its initialisation. Public static void main() > Main returns void. Allows simpler coding. Public static int main() > Returns an int. Is used to communicate with other programs. For C++: Main will always return an integer.
22nd Apr 2017, 3:42 AM
Kurt
Kurt - avatar
0
If you had a method that printed an array on the screen, for instance, a return value is pointless. You don't always want a return value because there isn't always a need for one.
12th Apr 2017, 12:58 PM
DaemonThread
DaemonThread - avatar
0
I use void methods when I need to change things without a return. for instance in my color picker program, when I change my hexadecimal, I need it to change my red green and blue values automatically. I don't need to have the values back. I just pass my hex into my method, it does some math to it, sets values to my variables, and that is all.
12th Apr 2017, 1:06 PM
LordHill
LordHill - avatar
0
please explain Does it mean eclipse can no longer be used to create android app let me know pls
12th Apr 2017, 7:09 PM
Ephraim Alebiowu
Ephraim Alebiowu - avatar
0
for example you don't have to return any thing using main function in c++ that's one use ......
19th Apr 2017, 1:25 PM
mikiyas tibebu
mikiyas tibebu - avatar
0
@miki u actually return int in main function of c++
20th Apr 2017, 9:19 PM
Haileyesus Shitalem
Haileyesus Shitalem - avatar
0
@ kurt u was referring c++ and u havw to return int
21st Apr 2017, 4:43 PM
Haileyesus Shitalem
Haileyesus Shitalem - avatar