what are you not able to do?
Note, this is a homework problem so lad the OP don't solve it for them.
B
what are you not able to do?
Just gave you an example use of pass by reference and how to call it.
Test it out see what happens!
#include <iostream>
void give_me_a_number(int & number)
{
number = 5;
}
int main()
{
int receiving_variable = 15000;
give_me_a_number(receiving_variable);
std::cout << "We received the number: "
<< receiving_variable
<< "\n";
}
should i just scrap everything i have done and start over? or is there some hope for what i did already? i really dont even know WHERE Its wrong. ugh
How might you extend lloyddean's code to return two separate numbers?
B
You might think of references a non global way to share a local variable with subroutine.
local variable to a function are not available to any code outside the function. Yet something magical just happened! What was it?
give_me_a_number(receiving_variable);
What was the result of running the example code?
#include <iostream>
using namespace std;
#include <cmath>
double polar (double &, double &, double, double);
int main ()
{
double & x, & y, r, angle;
cout << "Enter two numbers for polar coordinates: " << endl;
cin >> r, angle;
polar (x, y, r, angle);
cout << "The corresponding rectangular coordinates are (" << x << ", " << y << ")" << endl;
return 0;
}
double polar (double & x, double & y, double r, double angle)
{
x = r * cos(angle);
y = r * sin(angle);
return x, y;
}
So, what was it about the definition (or interface) of 'give_me_a_number' that allowed a LOCAL variable in main to be modified?
the (int & number)?
int main ()
{
double x;
double y;
double r;
double angle;