Question:
what does void function really do?
1970-01-01 00:00:00 UTC
what does void function really do?
Seven answers:
Alton W
2009-10-20 17:54:13 UTC
The examples are NOT void function. A [function returning ] void is purely procedural, as you said return nothing. Your examples are int functions (returning an int). Let me show you that you've answerd your own.



Here's an example of a function

int add(int num1, int num2)

{

}

num1 and num2 are FUNCTION PARAMETERS (or arguments the function will use) these are both of type int in this case.

so a call to add e.g. add(3, 4) will return 7.



Where as function

int DoSomething(void) means the funtion DoSomething will return an int but requires NO parameters (the keyword void means no objects).



Hence, thats the reason why int DoSomething(void) and int DoSomething() are the same.
tadpoleInSpace
2009-10-20 16:48:12 UTC
void means empty or nothing. It is used in return status or parameter as in your case. It just says this routine does not have any parameters. The two routines you have are identical. Some older compilers requires explicit definition for forward reference. The new compilers are pretty smart so they don't really care. So you can use either technique.
Deadly Tiger
2009-10-20 16:24:40 UTC
Void means nothing. Each time we use a function, we have to give the return type of it. So if we do not want to return any value to the calling function then we use "void".



Main is also a function, so we can use void.
2009-10-21 08:36:06 UTC
Hm....



















void : UNKNOWN TYPE

int main(void)

=> doesnt need arguments



int main (void x)

=> can not get an argument



int main (void* x)

=> can get an argument command, still unknown type, it can be int, float, char, string, etc. But it can be a value

int param = (int) x; // get an arg as integer type



int main (int x)

=> can get an argument as integer value

cout << x<
?
2009-10-21 04:53:47 UTC
I think you're confusing two uses of the keyword "void." The one you're using, int main( void ) says there are no arguments to the main() function, but that it produces an integer function value (in the case of main(), to the operating system and then to any calling batch files or shell scripts).



This is not, however, a void function. Such a function is defined this way:



void myFunc( xxxxx)

{

// processing

return; // NOTE: no return value.

}



In other words, a void function does not return a function value. This is perfectly permissible if the function produces an output result some other way, such as through an external static variable, an output parameter or physical output to a file or device. A function that produces absolutely no output is legal, but not very useful.



Hope that helps.
Pfo
2009-10-20 21:00:15 UTC
In C and C++, functions are allowed to return values to the function that called it. When a function is defined as having a return type of void, it indicates that functions does NOT return a value.



The void keyword is for your convenience, it tells the compiler to yell at you if you try to use a return value from a void function, or try to return a value from a void function. Other than that, it does nothing.
jplatt39
2009-10-20 09:39:18 UTC
Look at it this way: C/C++ by default pass variables by value. That means functions create new variables with the names given them in the parameter lists, and copy the values in whatever variables are sent as parameters to them. At the end of execution they destroy the values. This relates historically to common computer architecture at the time C and Unix (its twin) were created. Main() is an int function because the Operating System (especially if it is Unix or GNU/Linux) wants to know if it terminated normally. Sending return 0; tells the OS the program is over and all is well.



Languages like Pascal are similar to C but have procedures. C/C++ have void functions instead (because C was created by engineers who didn't want to waste time on frills). You use a procedure, or void function, in a couple of cases. If you want to do something like output data -- print it to screen, printer or disk but not otherwise change it, then that is one reason to choose a void function. You can still return an int or bool to suggest you did it successfully or not, but it is a legitimate use for a void.



Another is when you want to alter two possibly unrelated variables. What you pass the function is either a pointer in C * or their address in C++ & and instead of copying the value in the variable it creates it copies the address of the variable which in C you have to dereference and in C++ is dereferenced for you. If the function returns something, it can't return all the values that you send it at once, so it is legitimate to use pointers and addresses, and returning anything is probably a waste of time.



That's why you would use a void function.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...