I'm studying C++ beginner book for a while and now I'm learning a function call by reference.
I understand how but I don't understand why....
I solved the problem which is in my book.
Problem: Create a void function called round() that rounds the value of its double argument to the nearest whole value. Have round() use a reference parameter and return the rounded result in this parameter. You can assume that all values to be rounded are positive. Demonstrate round() in a program. Use modf() standard library function.
So. I wrote the following code.

#include <iostream>
#include <cmath>
using namespace std;
void round (double  &d);
int main()
{
 double userd;
 cout <<"Enter a floating value: ";
 cin >> userd;
 round ( userd );
 cout <<"Your floating value rounded up to : " << userd <<"\n";
 return 0;
}
void round (double &d)
{
 double intpart, fractpart;
 fractpart = modf(d, &intpart);
 if (fractpart >= 0.5)
  d = intpart + 1;
 else d = intpart;
}

But it also can write this way(without reference parameter):

#include <iostream>
#include <cmath>
using namespace std;
int round (double  d);
int main()
{
 double userd;
 int i;
 cout <<"Enter a floating value: ";
 cin >> userd;
 i = round ( userd );
 cout <<"Your floating value rounded up to : " << i <<"\n";
 return 0;
}
int round (double d)
{
 double intpart, fractpart;
 fractpart = modf(d, &intpart);
 if (fractpart >= 0.5)
  intpart += 1;
 
 return intpart;
}

The second code is more comfortable for me....
I understand in this case that book want me to practice writing a code for using reference parameter but I don't understand the benefit to use call by reference.
Please explain why I need to know the call by reference method.

Recommended Answers

All 6 Replies

>Please explain why I need to know the call by reference method.
Okay, return two values.

If I return two values, then I will see the answer of my question?
Could anybody give me some simple exercise for return two values?
(Easy one for beginner use!!)

>If I return two values, then I will see the answer of my question?
Probably.

>Could anybody give me some simple exercise for return two values?
Find the mean, median, and mode of an array of numbers. Then return the mean, median, and mode without storing them in another array.

Basically, call-by-reference functions allow the function to work on the actual variable (within main).

Call-by-value functions, copy the value of the variable into the function, with the original variable remaining unaffected.

The reason you would need to use call-by-reference will become apparent to you pretty soon.. for example write a program that gives the user change for a given coin ammount.. so if you start off with $2.95.. you get $2, 50c, 2x20c and 5c...
Function will look like this:
void give_change (int& amountLeft, int& numCoins, int coinValue);
So you will use the same function several times for different coin values, and the amountLeft (initially $2.95) will constantly get updated..
Try it out yourself.. it's like a classic intro example, according to my lecturer.. :)

The other reason has already been mentioned.. a function can't return more than one value..

oh and also it saves memory. If you pass by value it will copy the input object into local scope. So if you want some data just use a const reference to access it.

I'll try to write these program which Narue and phalaris_trip gave me. (Thank you)
Hopefully I'll be able to finish them and and post them on this board!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.