To VOID or not to VOID (a function) ?

Started by
8 comments, last by bluefox25 16 years, 12 months ago
I'm having the hardest time figuring out when to declare a function void or not. Does it make a great deal of difference, does it affect the result of the function. I know that a void function does not return a value but what exactly does that mean? If: int sum(int a,int b){ int c = a+b; return c:(is a return statement required?) } void sum(int a,int b){ int c = a+b; } Won't either function still give the same result(a+b)? Maybe I don't understand the difference if Im wrong. Can someone explain it to me if this is the case? Thanks Bluefox25
Advertisement
The correct terminology is that functions return values (instead of "giving" values). A void function does not return a value.

In your second example, the value of c is lost. In the first example, it is returned and can be used outside the function.

A return statement is mandatory in every control path for a non-void function (in C++, main automatically returns 0).
Think of it as though you've hired a contractor to do some work. Sometimes you'll want the contractor to hand you the final result of their work, and other times you don't really care about the result so long as the job is done. In the former your contractor is a function that returns a value (for example, int func()), in the later your contractor is a function that returns void (for example, void func()).

In your example, your first sum() function will return a value that is the sum of 'a' and 'b'. The second sum() however doesn't return anything, it does the work but you don't get to see the result. The variable 'c' in your second sum() function effectively disappears as soon as the function returns (ie. it's finished). It may be worth mentioning that even though the second function doesn't return a value, it still returns.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
So what your saying is that I wouldn't be able to cout the result of the void function?
Then what purpose does the void function serve if you can't use the result? If the result only lives within the function call, why use it or better yet, when would you use the void function?

Thanks!!!
:)
Quote:Original post by bluefox25
So what your saying is that I wouldn't be able to cout the result of the void function?
Then what purpose does the void function serve if you can't use the result? If the result only lives within the function call, why use it or better yet, when would you use the void function?

Thanks!!!
:)


It may do work which alters other parts of the program. For example it may call cout itself, create a window, write to a file or if you have global variables it could modify those. Later on you'll also learn that it's possibly to modify the parameters of a function.

As a simple example of a void function consider:
void report_error(){  std::cout << "The command given to the program was not recognized";}


A more real example would be a print function which takes a string and an integer as a parameter and breaks up the string into lines of length less than or equal to the integer and prints them separately. This is probably more complicated than what you can do now, but it's a good example of a useful void function.
Thanks for the info!! We are studying declaring/defining classes and structs and in the example, the prof uses void for alot of functions that calculate interest rates. I should go back and reread the section on void functions huh? :)
Quote:Original post by bluefox25
Thanks for the info!! We are studying declaring/defining classes and structs and in the example, the prof uses void for alot of functions that calculate interest rates. I should go back and reread the section on void functions huh? :)


What I imagine would be happenning here is that you have a class and are calling a member function of that class. The function itself may not be returning a value (and thus has a return type of 'void'), but it is changing the internal state (ie. member variables) of the class. So the results of the calculations are being stored, they just aren't being returned directly to the caller.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
int sum(int a,int b)
{
int c = a+b;
return c;
}
int result = sum(1,2) + sum(3, 4);//correct

void sum(int a,int b, int&c)
{
c = a+b;
}
int c1, c2;
sum(1, 2, c1);
sum(3, 4, c2);
int result = c1 + c2;//correct
Some languages differentiate between the two, sometimes using "procedure" and "function" keywords.

Procedure here is the form that doesn't return a value, whereas functon returns a value.

Neither of these is better, they each have very defined purpose. A function should generally be used where the return value will always be processed. Static code checkers and some compilers will warn you if you do not check the return value of a function.

Void functions or procedures have no use for return value. They modify global state.

Functions in computer language terminology are not the same as mathematical functions. They are just a way to organize blocks of code. A function can also modify values in much more obscure ways than just through return value. This falls under by value and by reference (or by pointer) semantics.
Thanks guys, I am always one to want to learn more and more. This is my first C++ class and I am enjoying it alot. I am thinking about getting a B.A. in Computer Science or maybe just a certificate in programming, don't know yet. If I were interested in creating games(xbox,ps,etc), what would be the better route?

This topic is closed to new replies.

Advertisement