0
Whats the point in making a method that returns nothing (void)?
12 Answers
+ 3
String name;
public void setName (String name ){
this.name=name;
}
+ 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
+ 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.
+ 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.
+ 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.
+ 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.
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.
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.
0
please explain
Does it mean eclipse can no longer be used to create android app
let me know pls
0
for example you don't have to return any thing using main function in c++ that's one use ......
0
@miki u actually return int in main function of c++
0
@ kurt u was referring c++ and u havw to return int