Good practice vs bad practice (void vs int functions)
Dear all,
I have a question regarding what a good practice is when working with functions. I am designing a very simple function for addition. I know that what will be the result of that addition is an integer, hence declaring an int function seems to be the right way of doing it. That said, I bumped into the fact that if I declared the same function as void when I run the program it seems to work all right!
here a contrast of what I've seen and found:
-------------------------------------------------------------------------------
int addition (int x, int y, int result){
result = x + y;
cout << "The result of your sum is " << result << endl;
return result;}
-------------------------------------------------------------------------------
void addition (int x, int y, int result){
result = x + y;
cout << "The result of your sum is " << result << endl;}
--------------------------------------------------------------------------------
Question here is: What is the difference and what implications can this kind of practice have in the long run?
Cheers!
FX
Re: Good practice vs bad practice (void vs int functions)
In both your examples, you print the result of the sum. Now, imagine if I wanted to use your function and write the result to a file, but without printing the result. I can't do that. But if you write your function as:
Code:
int addition(int x, int y) {
return x + y;
}
Then you can print:
Code:
cout << "The result of your sum is " << addition(100, 200) << endl;
and I can write to file without printing, and we both get to call your function. Furthermore, when you want to print, your second example can come into play:
Code:
void print_addition(int x, int y) {
cout << "The result of your sum is " << addition(x, y) << endl;
}
Re: Good practice vs bad practice (void vs int functions)
Right, I get it now. Now then, would you say that including "cout" calls within functions is correct or is there a rule of thumb to follow?
Re: Good practice vs bad practice (void vs int functions)
Quote:
Originally Posted by FelixCast
Now then, would you say that including "cout" calls within functions is correct or is there a rule of thumb to follow?
If your intention is to write a function whose purpose is to print to standard output, go ahead. Nothing wrong with that.
Later on, you may find that sometimes it is better to have a std::ostream reference parameter for certain functions so that you can opt to print to standard output, or to a file, or to some other output stream.
Re: Good practice vs bad practice (void vs int functions)
Quote:
Originally Posted by
FelixCast
Now then, would you say that including "cout" calls within functions is correct or is there a rule of thumb to follow?
There's an important design principle called Separation of Concerns that applies here. It's the basis of modularisation for example.
http://en.wikipedia.org/wiki/Separation_of_concerns
I'd say it's good practice not to intertwine output code with calculations. It's better to keep these "concerns" separated so each can be considered independently.
Re: Good practice vs bad practice (void vs int functions)
In addition to the SoC concept that Nuzzle mentioned is to consider an error handling strategy.
Should you return an error (probably not good if you are adding two integers)? Should you throw exceptions?
The main thing is to develop a consistent error strategy within your program so you can catch and report errors.
Re: Good practice vs bad practice (void vs int functions)
Thank you so much for your really good answers guys, it's brilliant to be a beginner and not get the snub from real programmers!