Why should I ever use anything other than a void function?

Basically the title says it all, but why should I use a double, int, or anything else? If you look at the function below, I could set it up to return a double. But it works just fine with a void type as well. I know there's a good reason for using specific return over void but I can't seem to find the answer.

1
2
3
4
5
6
7
8
	void Tax()
	{
		double taxrate = .10;

		SubTotal();
		cout << "Your Total is: " << subTotal.finalSubTotal*taxrate << " at a 10% tax." << endl;
		
	}
closed account (48T7M4Gy)
You don't have to return anything other than a void if you want to. But you are restricting yourself dramatically by not taking advantage of the full functionality and most functions you will have will have the added, but not insurmountable, complexity of passing at least some parameters by reference.

My guess is you would be the only person who does it that way but maybe that's in the nature of an outlaw.
I get what your asking. Logically, you should never need to return a value thanks to byReference variables. But when you actually get to the nuts and bolts it is much nicer to have return functions.

Suppose we lived in a world where everything was returned byVal. And I want to use a function to display the perimeter of a rectangle to a user.

Here's how we could do it with return functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int getPerimeter(int length, int width) 
{
      return (2 * length) + (2 * width)
}


int main()
{
   int length = 5; 
   int width = 4;
   
    cout << "The perimeter is: " << getPerimeter(length, width);
    return 0;
}


And here's how it would look without
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// One extra parameter.
void getPerimeter(int length, int width, int & perimeter) 
{
      perimeter = (2 * length) + (2 * width);
}


int main()
{
   int length = 5; 
   int width = 4;
   int perimeter = 0; // You don't need to give it an initial value to work but I would...
   getPerimeter(length, width, perimeter);
   cout << "The perimeter is: " << perimeter;
   return 0;
}



And that's two extra lines of code, and one extra parameter in the getPerimeter function. I'm not a professional so I can't say how much of a difference it makes. But imagine if you had a project where you 100 different functions just like this one. It would be a pain in the behind to handle it all. Simplicity really does matter.

And maybe you could have less code by setting your length or width variables equal to the perimeter, but that would just be really messy. So it's probably best to create a perimeter variable.
Last edited on
When talking about programming in general, there are functions and procedures. In C/C++, those are both called functions. A void function is a kind of procedure.

The difference is that the purpose of pure functions is to compute a value, while the purpose of procedures is to do things besides computing a value.

The common wisdom is that pure functions are preffered to procedures.

Here is why: it turns out that pure functions are simpler to use, combine, chain together, analyze, debug, etc... In fact, there are entire programming languages, called functional languages, which are designed around the idea that procedures should be avoided.

The problem with beginners is that many tutorials and courses start by introducing procedures before pure functions, or they use procedures heavily, and they don't explain why and how to use pure functions and procedures (or they do it badly and with bad examples), so the beginners get lost quickly. This is a common instructor's error, usually caused by him not having any experiece with functional programming style.

To get back to your question, it's hard to explain why pure functions are better than procedures in a few lines of text. Someone needs to demonstrate this to you, and it does take a dozen hours. It's not an easy concept to grasp, which is why many people don't get it.
Last edited on
Topic archived. No new replies allowed.