passing by reference

I'm teaching myself C++ (doing win32 console empty projects in VC++6) (no, this isn't homework.....I have 3 books and some online articles that i am trying to teach myself from) I am having a hard time with passing values by reference with functions.  I can do a pass by reference, but cant do this combination where you take the length entered and display it and also store it by reference also.
I have set up a simple problem so I can see the relationships.....the prob should do the following
using c++:

promt for you to enter length
display the length you entered
ask if you want to enter another (y or n)
if y then promt to enter length
display the length you entered
ask if you want to enter another (y or n) etc.....
if no, then display the total of all the lengths you entered (by using the pass by reference of the lengths that have been entered.

Psuedo code would be

main
{
get_data_function
display_function
total_function
}

get_data
{
}
display
{
}
total
{
}

Actual code and commenting as to why and where would be really helpfull also.

vernkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sureshkumarCommented:
I think this home work no doubt about it since it is getting data from user display on to monitor .Ok anyhow i am giving a hint to your question i.e., (take an array of size 1000 ask user and every time check it up y or n if n display all array values.)

Thanks
suresh kumar
0
vernkAuthor Commented:
well sureshkumar, i guess you could call it homework since I am trying to teach myself from books at home! I haven't got to arrays yet (but I have read about them) so, that wouldnt help me. But thanx for the hint......
0
vernkAuthor Commented:
well sureshkumar, i guess you could call it homework since I am trying to teach myself from books at home! I haven't got to arrays yet (but I have read about them) so, that wouldnt help me. But thanx for the hint......
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

sureshkumarCommented:
you said you are not come yet arrays.why thinking about this.First you learn basics then it will help to slove complex problems ok dont go for complex problems now.

array means
It is a collection of similar data type elements.
In c++
array definition is int a[10];
char ename[20];
0
vernkAuthor Commented:
sureshkumar, I have just finished a chapter on functions.....and at the end it shows pass by value, then pass by reference......I got one problem to work, but I don't have a good clear grasp of it, so i tried to write the above code, but when i try to get the total i just get the memory address....
0
graham_kCommented:
if you want to experiment,t he difference between pass by value & pass by reference, it's that changing the value of a parameter passed by reference will leave it changed when you return to the caller. If you pass by value, a local copy is taken within the function. You can change it, but when you rerun to caller it reverts to its original value.

int x = 4;

void fn(int y)
{
  y = 3;
 ....
}

void main()
{
  fn(x);
printf(" %d \n ", x);   //  gives 4, coz its pass by value
}


now ...
int x = 4;

void fn(int *y)   // note reference
{
  y = 3;
 ....
}

void main()
{
  fn(&x);   // pass address of x
printf(" %d \n ", x);   //  gives 3, coz its pass by reference & the fn() changed the value
}

this should help you understand the concept, without helping that particular question, just in case it _is_ homework :-)
0
vernkAuthor Commented:
graham k, I don't know C at all.....i have glanced at a book, but am lost with fn, printf, %d, *y,etc....could you do this in C++ code?
0
graham_kCommented:
C++ is the followon to C. First there was C, now there is C++. The pass by value/reference is (mostly) the same for both. At least, enough to leat you understand the concepts, since you are a total beginner (other experts, you can have the points, but please don't muddy the waters by debating this & confusing vernk).

fn() was a fucntion which I declared - I could have called it my_funciton() of anything else. Normally it has a meaningful name, like get_square_root() or compute_total(), etc.   You must know what funciutons are if you are looking at pass by value/reference. SOrry if I didn't make it clear enough.

printf() prints a value (defalut = to the screen). Don't worry about it too much (and read up on Cout).

Have a look at this little program which I wrote. It compiles ok, so just compile it & run it. I will admit that it is more C than C++, but since C is a subset of C++ that is acceptable. In any case, it will get the concept acorss.

Run it in your compiler. User the debugger. Follow the code & see how values change ...

#include <stdio.h>
#include <stdlib.h>

void pass_by_value(int y)
{
 y = 3;
}

void pass_by_reference(int * y)
{
  *y = 4;
}

int main(int argc, char *argv[], char *env[])
{
   int my_value = 5;

  printf("the value of 'my_value' is %d \n", my_value);

  pass_by_value(my_value);

  printf("the value of 'my_value' is %d \n", my_value);

  pass_by_reference(&my_value);          // pass the address of x

  printf("the value of 'my_value' is %d \n", my_value);

}
0
vernkAuthor Commented:
grahamk, Im going to take a shot at this (not trying to translate the c to c++)....
So if i

befor main i put

void get_data(float y, float &y);
void display(float refx)

then in main :
get_data(y, refx);
get_data(y, refx);
get_data(y, refx);
display(refx);

then make the function:

void get_data(float y, float refx)
{
cout <<"enter y's value";
cin >> y;
cout <<"the pass by value value of y is " <<y;
 refx=y;
cout <<endl;
}

void display(float refx)
{
cout <<"the pass by referance of y is " <<refx;
}

when it gets to display it will show me the sum of the three times y was entered?

0
graham_kCommented:
it looks like it.  And, yes, it looks like you are using the real C++ reference operator - the & on the parameter list. I think that you have grasped it now. Compile your program & run it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FrankdjCommented:
Hi,

A word of caution about doing this:

[snip]
void get_data(float y, float refx)
{
cout <<"enter y's value";
cin >> y;
cout <<"the pass by value value of y is " <<y;
 refx=y;
cout <<endl;
}

[end snip]

When you define a function with arguments, you are stating (in this case) that function get_data will be passed two variables, one is a float and the other is a reference to float.  You are trying to pass back a value through the function. In your example, "float y" is a temporary object and does not exist (i.e. goes out of scope) when you return from the function call.  Therefore, forgetting about other issues, "refx" would be referring to an object that no longer exists! Remember that a reference is just an alias (another name for) some variable or object.  Your function should not incorporate references at all.  Instead, redefine it as:

float get_data(void)
{
float y;

cout <<"enter y's value ";
cin >> y;
cout <<"the pass by value value of y is " <<y;
cout <<endl;
return y;
}

and somewhere in main(), where you call this function, assign the return value to a float variable.

..
..
..
int main()
{
 float savefloat;

 savefloat = get_data();  
..
..
..

To investiage references, I would suggest that you define a few variables in main and assign them values, then pass those to a function which attempts to change their values. Such as;

 void change_var1(float temp)
 {
  temp = 11.1;
 }
 void change_var2(float &temp)
 {
  temp = 11.1;
 }

int main()
{
 float y;
 y=20.1;
 cout << "Value of y is " << y << endl;
 change_var1(y);
 cout << "Value of y is " << y << endl;
 change_var2(y);
 cout << "Value of y is " << y << endl;
 
}

In the first function call, you will see that only the value of "y" is changed in the body of the function, but that its value in main is not affected.  However, in the second function call, you will note that the value of "y" is changed in main also. This effect is similar to what would occur if you had defined the function to take a pointer to float.  The difference is that there is no dereferencing involved, the compiler takes care of it for you.

Hope this helps!

Regards,
Frank DiJoseph
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.