473,226 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

advantage of returning values through the argument list

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
4 1794
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Robert Fitzpatrick | last post by:
Can someone point me to some more information or perhaps show an example of returning a recordset from a plpgsql function. I'd like to send an argument or arguments to the function, do some queries...
2
by: Noah | last post by:
Is there a simple way to get a dictionary of argument names and their default values for a method or function? I came up with one solution, but I feel like Python must have a simpler way. ...
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
9
by: josh | last post by:
Hi, I'm converting (for learning purpose) )a program from Java to C++ and I've a doubt: In Java every argument (variable and reference types) is passed and returned in functions by-value so if I...
4
by: bullockbefriending bard | last post by:
Given: class Z(object): various defs, etc. class ZList(list): various defs, etc. i would like to be able to replace
7
by: arnuld | last post by:
/* C++ Primer - 4/e * * 1st example from section 7.2.2, page 234 * returning 2 values from a function * * STATEMENT: * to find a specific value in a vector and number of times * that...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.