• 15
• 15
• 11
• 9
• 10

To VOID or not to VOID (a function) ?

This topic is 3988 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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).

Share on other sites
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.

Share on other sites
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!!!
:)

Share on other sites
Quote:
 Original post by bluefox25So 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.

Share on other sites
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? :)

Share on other sites
Quote:
 Original post by bluefox25Thanks 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.

Share on other sites
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

Share on other sites
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.