From: neil on
I'm working on a program that obtains user input of 4 int values.
These values must then be passed to a function which carries out it's
task (there is no return to the main function).

Is it best to pass these values by reference or by value?

Passing values to function by value is confusing because the function has
another name for the object in the main program. I understand that using
references is useful if the data is very large so replication would be
avoided. Are there other rules which govern when a reference or call by
value should be used?

---------

This is part of my program:
// a driver program and function to plot a horizontal line of pixels

#include <iostream>
#include 'playpen.h'

using namespace std;

// function declaration - would references be better here?
void horiz(int x, int y, int number, int color);

int main()
{
// get user input
int x(0), y(0);
int no(0);
int col(0);


cout << "Input the x , y coordinates of where this is to begin\n";
cin >> x >> y;
cout << "Input the number of pixels to be in the line \n";
cin >> no;
cout << "What color would you like? (0 to 255) \n";
cin >> col;

// setup playpen
fgw::playpen paper;
horiz();
}

// function definition
void horiz(int x, int y, int number, int color)
{
// not yet defined
}



From: Francesco S. Carta on
neil <invalid(a)invalid.net>, on 06/08/2010 12:41:06, wrote:

> I'm working on a program that obtains user input of 4 int values.
> These values must then be passed to a function which carries out it's
> task (there is no return to the main function).
>
> Is it best to pass these values by reference or by value?

You normally pass objects by value when those objects are small built-in
types, because the cost is trivial even when the compiler does not
optimize the "additional" variables away.

Passing by reference could be better for large objects whose copying
process can be expensive - you also pass by reference (or by pointer)
when you need to modify the passed value and you don't want to use the
return value for that purpose.

Some things in C++ should only be passed by reference, because of their
innate nature (iostreams, for example).

> Passing values to function by value is confusing because the function has
> another name for the object in the main program.

Actually, the fact that something has a name in the function and another
name at the calling site can solve confusion, instead of creating it,
but moreover this is matter of style and tastes.

Also, how would you use a function on different objects if you were
forced to name them always with the same name? You'd have to go through
a fair bit of loops and hoops in order to achieve something useful.

> I understand that using
> references is useful if the data is very large so replication would be
> avoided.

Although I agreed with this in my second paragraph above, even large
objects can sometimes be optimized by the compiler... since this is
something that varies between different implementations, this matter is
a common source of debate - this is my perception, at least.

> Are there other rules which govern when a reference or call by
> value should be used?

I'm not aware of any absolute rule apart from the fact that you have no
other choice but pass by reference or by pointer, if you want to let the
modification of that parameter to be seen outside of the function scope
(other than returning it as a result of the function, of course).

>
> ---------
>
> This is part of my program:
> // a driver program and function to plot a horizontal line of pixels
>
> #include<iostream>
> #include 'playpen.h'

My compiler would refuse the above include directive, expecting double
quotes instead of single ones. Which compiler are you using?

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
From: neil on
On Fri, 06 Aug 2010 15:07:54 +0200, Francesco S. Carta wrote:

> neil <invalid(a)invalid.net>, on 06/08/2010 12:41:06, wrote:
>
>> I'm working on a program that obtains user input of 4 int values. These
>> values must then be passed to a function which carries out it's task
>> (there is no return to the main function).
>>
>> Is it best to pass these values by reference or by value?
>
> You normally pass objects by value when those objects are small built-in
> types, because the cost is trivial even when the compiler does not
> optimize the "additional" variables away.
>
> Passing by reference could be better for large objects whose copying
> process can be expensive - you also pass by reference (or by pointer)
> when you need to modify the passed value and you don't want to use the
> return value for that purpose.
>
> Some things in C++ should only be passed by reference, because of their
> innate nature (iostreams, for example).
>
>> Passing values to function by value is confusing because the function
>> has another name for the object in the main program.
>
> Actually, the fact that something has a name in the function and another
> name at the calling site can solve confusion, instead of creating it,
> but moreover this is matter of style and tastes.
>
> Also, how would you use a function on different objects if you were
> forced to name them always with the same name? You'd have to go through
> a fair bit of loops and hoops in order to achieve something useful.
>
>> I understand that using
>> references is useful if the data is very large so replication would be
>> avoided.
>
> Although I agreed with this in my second paragraph above, even large
> objects can sometimes be optimized by the compiler... since this is
> something that varies between different implementations, this matter is
> a common source of debate - this is my perception, at least.
>
>> Are there other rules which govern when a reference or call by value
>> should be used?
>
> I'm not aware of any absolute rule apart from the fact that you have no
> other choice but pass by reference or by pointer, if you want to let the
> modification of that parameter to be seen outside of the function scope
> (other than returning it as a result of the function, of course).
>
>
>> ---------
>>
>> This is part of my program:
>> // a driver program and function to plot a horizontal line of pixels
>>
>> #include<iostream>
>> #include 'playpen.h'
>
> My compiler would refuse the above include directive, expecting double
> quotes instead of single ones. Which compiler are you using?

In this the case of my simple program here would you say that its not
right to use references then? Is my approach correct?
From: Francesco S. Carta on
neil <invalid(a)invalid.net>, on 06/08/2010 13:55:24, wrote:

> On Fri, 06 Aug 2010 15:07:54 +0200, Francesco S. Carta wrote:
>
>> neil<invalid(a)invalid.net>, on 06/08/2010 12:41:06, wrote:
>>
>>> I'm working on a program that obtains user input of 4 int values. These
>>> values must then be passed to a function which carries out it's task
>>> (there is no return to the main function).
>>>
>>> Is it best to pass these values by reference or by value?
>>
>> You normally pass objects by value when those objects are small built-in
>> types, because the cost is trivial even when the compiler does not
>> optimize the "additional" variables away.

<snip>

> In this the case of my simple program here would you say that its not
> right to use references then? Is my approach correct?

Right for me, yes, I would use plain by-value arguments in case of
simple ints and the alike, just like you have done. Using references in
this case would seem an overkilling over-typing for me.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
From: Francis Glassborow on
neil wrote:
> I'm working on a program that obtains user input of 4 int values.
> These values must then be passed to a function which carries out it's
> task (there is no return to the main function).
>
> Is it best to pass these values by reference or by value?

Unless the function is intended to modify the original value there is no
profit in passing builtin types by reference (which should be a const
reference if the function does not modify the original) just pass by value.

>
> Passing values to function by value is confusing because the function has
> another name for the object in the main program. I understand that using
> references is useful if the data is very large so replication would be
> avoided. Are there other rules which govern when a reference or call by
> value should be used?

You pass by (non const) reference when it is intended that the function
modifies the original. That is why playpen is passed by reference.

>
> ---------
>
> This is part of my program:
> // a driver program and function to plot a horizontal line of pixels
>
> #include <iostream>
> #include 'playpen.h'
>
> using namespace std;
>
> // function declaration - would references be better here?
> void horiz(int x, int y, int number, int color);
>
> int main()
> {
> // get user input
> int x(0), y(0);
> int no(0);
> int col(0);
>
>
> cout << "Input the x , y coordinates of where this is to begin\n";
> cin >> x >> y;
> cout << "Input the number of pixels to be in the line \n";
> cin >> no;
> cout << "What color would you like? (0 to 255) \n";
> cin >> col;
>
> // setup playpen
> fgw::playpen paper;
> horiz();
and your function needs arguments to match the parameters in the
declaration.

> }
>
> // function definition
> void horiz(int x, int y, int number, int color)

This function needs access to the playpen object and so should have a
reference parameter to playpen.


> {
> // not yet defined
> }
>
>
>

--
Note that robinton.demon.co.uk addresses are no longer valid.