Ask your own question, for FREE!
Computer Science 23 Online
OpenStudy (anonymous):

You know what I don't understand... the concept of a null function in C++? That is, why do we have to use void function vs. int function? Why is it in void functions, you still have to put a return variable; Shouldn't it just be return 0; if it's void? What does void really mean if the function is calculating an operation and it's supposed to be spitting out a value to the main function?

OpenStudy (anonymous):

Functions whose return type is void are not supposed to have any value in an expression when called, and in C++ you cannot use them in an expression the way I am doing in this code: http://ideone.com/saNKG You would typically use a void function over an int function if you are writing simple procedures, like wrapper functions, where you have no need for the function to return any value.

OpenStudy (shadowfiend):

It's also worth mentioning that void functions should not *require* a return statement, as far as I know. You only need a return statement in a void function if you want to return early (i.e., if you want to skip the rest of a function body based on a given condition).

OpenStudy (anonymous):

I forgot to mention that :( if you do not include a return statement in your void function, then it 'returns' after the very last line is executed, 'falling off' the code block.

OpenStudy (anonymous):

But you see what I mean? I'm confused in the example you gave me agdgd... (long name lol) isn't the value of sum being returned in the function? Why is it void? Isn't it spitting out a value? Or..?

OpenStudy (anonymous):

It isn't spitting out any value at all, which might seem confusing.

OpenStudy (anonymous):

Or is it only spitting out a value when it's being called ... in the calling function, so it doesn't count as returning a value?

OpenStudy (anonymous):

The sum() function in my example does not spit out any value wherever it is called, since it has a return type of void.

OpenStudy (anonymous):

unless you're talking about pointers to functions that return void :-P

OpenStudy (anonymous):

So, the calling function calls the called function sum() and it carries out the "operation of summation/function of sum() " in the main function????

OpenStudy (anonymous):

Can you explain to me what you mean by pointers to functions that return void?

OpenStudy (anonymous):

Actually, what happens is that a new frame is created on the stack, belonging to the sum function, where the operation takes place but no value is carried over to main's frame in the stack.

OpenStudy (anonymous):

Ohh that's strange o.O So, when do you use void and when you do you int or double for a function?

OpenStudy (anonymous):

I can take your sum function and make it return a int value though right? What would be the difference between void sum() and int sum() then?

OpenStudy (anonymous):

There would be a huge difference if you change my example that way: http://ideone.com/clone/saNKG

OpenStudy (anonymous):

instead of an error where the compiler screams at you for using the value of a function that returns void, the sum function actually returns the sum of the arguments passed to it whenever it is called.

OpenStudy (anonymous):

So, you use int or double (Basically anything not void) if you want the function to return a value to the calling function. But you only use void if you don't want to return a value? It's strange because sometimes I see it used when you do want to return a value I think... hmmm

OpenStudy (anonymous):

right

OpenStudy (anonymous):

Thank you!! ! I think I understand it a lot better now :D

OpenStudy (shadowfiend):

If you see it used when people do want to use it to return a value, it's possible you're seeing a void pointer (void * rather than just void). That's... A whole topic unto itself :p

OpenStudy (anonymous):

well it's not really good example just to calculate sum in void function :D however you can use void function to print some stuff, for example same sum of two numbers and you don't need to return anything because it's just prints to standard output sum of two numbers http://ideone.com/clone/saNKG

OpenStudy (anonymous):

I see void when I'm using pass by reference.... Is that an exception too?

OpenStudy (anonymous):

#include<iostream> using namespace std; void calc(double, double, double, double, double); int main() { double firstnum, secnum, thirdnum, sum, product; cout << "Enter three numbers: "; cin >> firstnum >> secnum >> thirdnum; calc(firstnum, secnum, thirdnum, sum, product); cout << "\nThe sume of the numbers is: " << sum << endl; cout << "The product of the numbers is: " << product << endl; return 0; } void calc(double num1, double num2, double num3, double total, double product) { total= num1 +num2+ num3; product= num1 * num2 * num3; return; }

OpenStudy (anonymous):

^Above I removed pass by reference (&) just to see the difference and it won't compute the sum or product properly... I wonder why? Is it because of void? If I add:

OpenStudy (anonymous):

#include<iostream> using namespace std; void calc(double, double, double, double&, double&); int main() { double firstnum, secnum, thirdnum, sum, product; cout << "Enter three numbers: "; cin >> firstnum >> secnum >> thirdnum; calc(firstnum, secnum, thirdnum, sum, product); cout << "\nThe sume of the numbers is: " << sum << endl; cout << "The product of the numbers is: " << product << endl; return 0; } void calc(double num1, double num2, double num3, double& total, double& product) { total= num1 +num2+ num3; product= num1 * num2 *num3; return; }

OpenStudy (anonymous):

It works fine.

OpenStudy (anonymous):

Actually what's interesting is even if I change void to double and make it return a value, it wouldn't return the correct value.

OpenStudy (shadowfiend):

If you pass something by reference, assigning a value to it assigns the value to the original variable in the other function. That's why it works when you add the reference. If you don't add the reference, then calc is working with a copy of the values from main, and these copies have no link whatsoever to the variable in main. Thus, assigning to them changes their values in calc, but not in main. As for returning, I don't know if you're returning the product or sum. The return value is entirely separate from the parameters.

OpenStudy (rsmith6559):

void is used to satisfy syntactic requirements. In English, when you declare a function, you need a return type, the function name, and argument types and names in a comma separated list surrounded by parentheses. If the function doesn't return anything, you'd have a syntax error, so you need something to put in there, void. void is also used by malloc to return an untyped ( in theory can't be used until it's cast to a type ) pointer to allocated memory.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!