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.