6

I am kind of newbie in using c++. I have a quick question, probably a dumb question.

streamsize prec = cout.precision(3);

As I understand correctly this declaration works like that: set the cout precision to 3, but assign the previous precision value to prec.

Also, simply, we can assign a function result (say a math addition function) to a variable:

int z = addition(3,4);

In the second one, it does the calculation and assigns the results to the variable z, not the previous value or a default value. Is my understanding correct? What is the difference between them?

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
user2371160
  • 107
  • 1
  • 6

1 Answers1

4

What value a function returns depends entirely on that particular function. Most functions simply return a result of their operation.

The state-setting functions in standard library streams (such as precision) are a bit unusual in their interface of "I set a new value and return the old one," but it's still perfectly valid, as long as the function's behaviour is documented (which it is in their case).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    So, it depends on the function, and its particular behavior in returning a value. cout.precision just returns the previous value. then to change the precision and assign it to prec I should do the following. std::cout.precision(3); streamsize prec = cout.precision(); Thanks a lot. – user2371160 May 10 '13 at 19:07