1

I was reviewing some code to test run for myself that I read in a textbook, here it is:

#include <iostream>
#include <fstream>
using namespace std;

const double PI = 3.1415926535897932;

void area_of_circle(const double R, double& L, double& A);

int main() {
  const int N = 10;
  double R[N];
  double area, perimeter;
  int i;

  for (i = 0; i < N; i++) {
    cout << "Enter radius of circle: ";
    cin >> R[i];
    cout << "i= " << (i+1) << " R(i)= " << R[i] << "\n";
  }

  ofstream myfile ("AREA.txt");
  for (i = 0; i < N; i++){
      area_of_circle(R[i], perimeter, area);
      myfile << (i+1) << ") R= " << R[i] << " perimeter= "
      << perimeter << "\n";
      myfile << (i+1) << ") R= " << R[i] << " area= "
      << area << "\n";
    }

    myfile.close();
}

void area_of_circle(const double R, double& L, double& A) {
  L = 2.0*PI*R;
  A = PI*R*R;
}

What I did differently to what the author did was not reference my const double R parameter. He included the & reference symbol (const double& R) in the area_of_circle function. I ran the code trying it with and without the reference symbol and i received the same results for both.

Essentially my questions is, why did this author include it if they both yield the same answer? I chose to not include it because my understanding is why include it if R is not changing throughout the functions computation.

n0de
  • 155
  • 4
  • 14
  • 1
    You are right. And your instructor poorly named the function. – llllllllll Mar 09 '18 at 06:41
  • @liliscent Aha! That too! Thank you Edit: I meant author, not instructor, oops! – n0de Mar 09 '18 at 06:42
  • Also read this https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – llllllllll Mar 09 '18 at 06:43
  • In this example it doesn't make big difference in time of execution, but if you will pass whole table as copy it will may have significant influence on execution time of your program, passing by const reference will not cause copying of memory. – BartekPL Mar 09 '18 at 06:44
  • @liliscent Will do thank you – n0de Mar 09 '18 at 06:45
  • @BartekPL Hm, good to know. I'll keep that in mind with larger data. Thanks – n0de Mar 09 '18 at 06:45
  • 1
    Did you really try it without reference? Without the & in the type, arguments are passed "by value" and are modified only on the function stack, Thus the computed values will not be available outside the area_of_circle function. In C there are only two ways to return values from a function: 1. Return it as the function return type (which is not possible in your case because the function is void). 2. Pass arguments by reference. – Victor Havin Mar 09 '18 at 07:01
  • 1
    @juanchopanza Why you said that version that is above will not work, as far as R is not modified this code seems work – BartekPL Mar 09 '18 at 07:16
  • @BartekPL Yes, it seems I misread the question, sorry! – juanchopanza Mar 09 '18 at 07:33
  • @VictorHavin In this case I only want to return values for L and A, not R, since R is not being modified. That's why I didn't include the reference. – n0de Mar 09 '18 at 07:35
  • Who knows what the author was thinking? That is a lousy program to emulate, for a number of reasons. Get a better book. (What's the name of the book, BTW?) – Jive Dadson Mar 09 '18 at 07:48
  • @n0de. Oh, I see. If you do not need to modify a variable, you don't have to pass it by ref. Besides, the const qualifier wouldn't let you modify R anyways. Another thing to consider when passing arguments is thread safety. Passing parameters by ref is not always thread safe. – Victor Havin Mar 09 '18 at 19:15
  • @VictorHavin Oh ok, I won't use const then, mental note. Also, that's interesting. What would you recommend as an alternative? – n0de Mar 09 '18 at 19:34
  • @JiveDadson Why is it lousy? Looks like it does what you want it to do? – n0de Mar 09 '18 at 19:36
  • @n0de. It is OK to use const if you want to make sure the value doesn't get changed. In your particular case const is good. The radius should remain unchanged in the scope of this function. If by mistake you try to assign a different value to it, compiler will give you an error message. – Victor Havin Mar 09 '18 at 21:12

1 Answers1

2

In example provided by you usage or not of reference with R parameter doesn't make big difference in time of execution, because parameter is small (single double), but if you will pass whole table as copy (not reference) it will may have significant influence on execution time of your program.

Passing by const reference will not cause copying of memory. It only references to memory where parameter is stored. const is used to avoid modification of parameter passed by reference. If you pass simple parameter (like double) to function you needn't the const, because you can only modify the copy.

Check this one too : C++: Argument Passing "passed by reference"

BartekPL
  • 2,290
  • 1
  • 17
  • 34
  • 2
    Keep in mind that for small stuff pass by copy is faster than pass by reference, especially on modern calling conventions (where most parameters are passed through registers). This comes down to the fact that pass by reference has its costs - namely, indirection cost and barriers that it poses to the optimizer. – Matteo Italia Mar 09 '18 at 06:56
  • 1
    @juanchopanza I said only about `R` parameter as about it was the question – BartekPL Mar 09 '18 at 07:17
  • @BartekPL So in a case where you don't want to modify the object as well as avoid copying it, i could just use ie const double R, as I have done, correct? So with larger data, why wouldn't I continue to use const? Since it doesn't copy memory and only references/doesn't modify? – n0de Mar 09 '18 at 07:34
  • @n0de Sorry, I don't understand your question :/ It seems that you want to know if const is still needed. Please, read this question and answers : https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters – BartekPL Mar 09 '18 at 07:39