By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

advantage of returning values through the argument list

P: n/a
Hi. I have found one advantage of returning values through the argument
list. It's that we have to store the return value. But when we return by
value, we may forgot to store the return value.
Consider,

void f(int x, int& i);
int i, j;
f(1, i);
f(2, j);
But

int f(int x);
int i, j;
i = f(1);
j = f(2);
Generally, return by value functions leads to code that is easier to read.
This appears to be the case in the short example above. But with return by
value functions, we may forget to store the return value in 'i' or 'j'.
With returning functions through the function argument list, this is
impossible.

This becomes more important when writing more complicated code, where we
might assign to the same variable twice.

result = f(3);
if (!result) {
result = f(4);
}

Seems somewhere we might just write "f(4);" rather than "result = f(4);".

Any comments?
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Siemel Naran posted:
Hi. I have found one advantage of returning values through the
argument list. It's that we have to store the return value. But when
we return by value, we may forgot to store the return value.
Consider,

void f(int x, int& i);
int i, j;
f(1, i);
f(2, j);
But

int f(int x);
int i, j;
i = f(1);
j = f(2);
Generally, return by value functions leads to code that is easier to
read. This appears to be the case in the short example above. But with
return by value functions, we may forget to store the return value in
'i' or 'j'. With returning functions through the function argument
list, this is impossible.

This becomes more important when writing more complicated code, where
we might assign to the same variable twice.

result = f(3);
if (!result) {
result = f(4);
}

Seems somewhere we might just write "f(4);" rather than "result =
f(4);".

Any comments?

With respect, I think your argument is bullshit. Next thing we'll have
compilers saying:

Warning: line 5: x += 365
: Are you sure? Because this year is a leap year
: Perhaps you meant 366?
There only advantage I recognize with your method, I shall illustrate it in
the following:

Firstly, here's the class I'll work with. You don't have to pay too much
attention to this part, skip on to the actual "main" code:
#include <iostream>
#include <cstdlib>

class AnyClass
{
public:

unsigned play_member;

private:

static unsigned object_counter_PRIVATE;
char* name_PRIVATE;

public:

static unsigned const &object_counter;
const char* const &name;

private:

bool const has_dynamically_allocated_name;

public:

AnyClass(const char* const in_name) : has_dynamically_allocated_name
(false), name(name_PRIVATE)
{
const_cast<const char* &>(name_PRIVATE) = in_name;

++object_counter_PRIVATE;
std::cout << " AnyClass Constructor for: " << name_PRIVATE <<
'\n';
}

AnyClass(AnyClass const &original) : has_dynamically_allocated_name
(true), name(name_PRIVATE)
{
std::size_t length = std::strlen( original.name_PRIVATE );

name_PRIVATE = new char[length + 1 + 10]; //1 for the null
character

memcpy( name_PRIVATE, "Copy of \"" , 9 );

memcpy( &name_PRIVATE[9], original.name_PRIVATE, length );

name_PRIVATE[ 9 + length ] = '\"';
name_PRIVATE[ 9 + length + 1 ] = '\0';

++object_counter_PRIVATE;
std::cout << "AnyClass Copy Constructor for: " << name_PRIVATE <<
'\n';
}

~AnyClass()
{
std::cout << " AnyClass Destructor for: " << name_PRIVATE <<
'\n';

if (has_dynamically_allocated_name) delete [] name_PRIVATE;
--object_counter_PRIVATE;
}
};

unsigned AnyClass::object_counter_PRIVATE = 0;
unsigned const &AnyClass::object_counter = AnyClass::object_counter_PRIVATE;

And now here's the code:

AnyClass Function1()
{
AnyClass poo = AnyClass("poo"); //Just for kicks!

poo.play_member = 6;

poo.play_member += 32;

return poo;
}

void Function2(AnyClass &in)
{
in.play_member = 6;

in.play_member += 32;
}

int main()
{
Function1();

AnyClass monkey("monkey");
Function2(monkey);
}

When I compile it with "g++ -ansi -pedantic", it prints the following:

AnyClass Constructor for: poo
AnyClass Destructor for: poo
AnyClass Constructor for: monkey
AnyClass Destructor for: monkey
But... when I compile it with "g++ -ansi -pedantic -fno-elide-constructors",
it prints the following:
AnyClass Constructor for: poo
AnyClass Copy Constructor for: Copy of "poo"
AnyClass Destructor for: poo
AnyClass Copy Constructor for: Copy of "Copy of "poo""
AnyClass Destructor for: Copy of "poo"
AnyClass Destructor for: Copy of "Copy of "poo""
AnyClass Constructor for: monkey
AnyClass Destructor for: monkey
....simulating a shit compiler.
So there's *one* advantage with your method.

Then there's the *dis*advantage - you can't work with the constructor.

-JKop
Jul 22 '05 #2

P: n/a
Siemel Naran wrote:
[snip] result = f(3);
if (!result) {
result = f(4);
}

Seems somewhere we might just write "f(4);" rather than "result = f(4);".


Seems like you are saying that for the same argumentation the language
should force us to write

add( i, 2 + 5 );

instead of

i = 2 + 5;

because someone might 'forget' to use the result of the addition.
Seriously: In 20 years I have never 'forgotten' to use the return
value of a function if I needed that return value for something.
And believe me, I have 'forgotten' a lot of things to do. But never
to assign the return value if I needed it for other calulations or
I/O or storing in data base.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a
int main()
{
Function1();

AnyClass monkey("monkey");
Function2(monkey);
}

I'm such a fool! Should've been

int main()
{
AnyClass cheese = Function1();

AnyClass monkey("monkey");
Function2(monkey);
}
recompiling I get:

g++ -ansi -pedantic
AnyClass Constructor for: poo
AnyClass Constructor for: monkey
AnyClass Destructor for: monkey
AnyClass Destructor for: poo

g++ -ansi -pedantic -fno-elide-constructors

AnyClass Constructor for: poo
AnyClass Copy Constructor for: Copy of "poo"
AnyClass Destructor for: poo
AnyClass Copy Constructor for: Copy of "Copy of "poo""
AnyClass Destructor for: Copy of "poo"
AnyClass Copy Constructor for: Copy of "Copy of "Copy of "poo"""
AnyClass Destructor for: Copy of "Copy of "poo""
AnyClass Constructor for: monkey
AnyClass Destructor for: monkey
AnyClass Destructor for: Copy of "Copy of "Copy of "poo"""
-JKop
Jul 22 '05 #4

P: n/a
g++ -ansi -pedantic
AnyClass Constructor for: poo
AnyClass Constructor for: monkey
AnyClass Destructor for: monkey
AnyClass Destructor for: poo

2 objects created. No temporaries. Yipee!

g++ -ansi -pedantic -fno-elide-constructors

AnyClass Constructor for: poo
AnyClass Copy Constructor for: Copy of "poo"
AnyClass Destructor for: poo
AnyClass Copy Constructor for: Copy of "Copy of "poo""
AnyClass Destructor for: Copy of "poo"
AnyClass Copy Constructor for: Copy of "Copy of "Copy of "poo"""
AnyClass Destructor for: Copy of "Copy of "poo""
AnyClass Constructor for: monkey
AnyClass Destructor for: monkey
AnyClass Destructor for: Copy of "Copy of "Copy of "poo"""

5 objects created, 3 of which are temporaries.
-JKop
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.