By Ryan
(I added a few comments to your code to make it a little more readable, btw)
// Kevin Atkinson
// Chapter 7 Opportunity
// include headers
#include <iostream.h>
These prototypes (may) need to be updated to match the changes I'm going to mention below to these functions.
// function prototypes
double calc();
void display();
int main()
{
int i, j, k;
cout << "Enter the first integer: ";
cin >> i;
cout << "Enter the second integer: ";
cin >> j;
cout << "Enter the last integer: ";
cin >> k;
If you are going to pass variables between functions, then you will have to pass each variable BY REFERENCE - yes, this is important.
I'm not sure how familiar you are with by reference vs by value, but here's the general idea: when you pass by value and access the variable within the function, it does what you'd expect. Internally, inside the compiler, what is happening is the value is sent, not the actual variable (a copy is made). So, if you try and modify the copy, nothing will "save" because you are modifying the copy which has no "link" back to the original.
If you pass a variable by reference, it works a little differently. Instead of the compiler sending just the value, a reference to the location in memory is sent instead. What does this mean? Because you have access to the memory location of the original and not a copy if you try and modify the contents of the variable your changes will be "saved".
Passing by reference is what you want if you are doing this activity by passing around values.
The syntax for passing by reference:
#include <iostream.h>
using namespace std;
int add_value(int &value) {
value = 100;
}
int main() {
int some_number;
add_value(&some_number);
cout << some_number << endl;
}
This will print 100
. Got it? Cool.
// pass i, j, k, sum, and product by reference here to both
// calc (sum and product are needed so calc can "save" into
// them). Don't worry about passing values by reference into
// display - it only needs to read the values and doesn't need
// to write to them.
calc();
display();
// pause and return
system("pause");
return 0;
}
So, a couple things here - you don't need to redefine a
, b
and c
.
a
, b
, and c
really are just i
, j
, and k
, so all you need to do
is to pass them (also sum
and product
) in as arguments
(calc(int i, int j, int k, double &sum, double &product)
) - notice how I
put an &
before the variable - that means to pass by reference. Only sum
and
product
are being passed by reference because they are the only 2 that you need
to update within the function.
Also, you can only return once from a function. Any return
s after the first
one are ignored. Similarly, because you are passing all the variables that
are to be updated by reference, the return type of double
is superfluous. I'd
just use void
to save some memory.
double calc()
{
int sum, a, b, c;
sum = a + b + c;
return (sum);
int product;
product = a * b * c;
return (product);
}
Lastly, you'll need to pass in i
, j
, k
, sum
, and product
into this
function (by reference isn't necessary because you only need the value of the
variable, and don't need to update it). Also, update your cout
s to use the
updated variable values that you passed into the function.
void display()
{
int e, f, g, total, multiply;
cout << "The numbers you entered were " << e << ' ' << f << ' ' << g << endl;
cout << "The sum of the numbers is " << total << endl;
cout << "The product of all of the numbers is " << multiply << endl;
}
And, I think that's it! Keep in mind I'm doing this without a compiler in front of me so some of my code examples aren't perfect, but I think they are close enough to prove the concept.
😄