473,320 Members | 2,122 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,320 software developers and data experts.

function return value

Hello All,
I know that when you pass an argument to a function (if you want let
the
function be able to change the value) then you can choose to pass the
argument by reference or a pointer to that argument.

Now my question is, why would you prefer to let a function return a
reference or a pointer ???
Consider the following with operator overloading. I suppose the
operator can be seen as a function (right ?)

type operator - ()
type& operator - ()

Thanks for explaining this.
Robert

Jul 23 '05 #1
4 4273
Hello,
Passing parameters by reference is not only used for modification purpose.
It's used also to avoid copying for performance issue.You return an argument
by reference for the same reason (be carefull to not returning a refrence to
a temporary )
Ahmed MOHAMED ALI
wo*********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello All,
I know that when you pass an argument to a function (if you want let
the
function be able to change the value) then you can choose to pass the
argument by reference or a pointer to that argument.

Now my question is, why would you prefer to let a function return a
reference or a pointer ???
Consider the following with operator overloading. I suppose the
operator can be seen as a function (right ?)

type operator - ()
type& operator - ()

Thanks for explaining this.
Robert

Jul 23 '05 #2
wo*********@yahoo.com wrote:
I know that, when you pass an argument to a function
(if you want let the function be able to [modify] the value)
then you can choose to pass the argument
by reference or a pointer to that argument.

Now my question is,
"Why would you prefer to let a function return a reference or a pointer?"

Consider the following with operator overloading.
I suppose the operator can be seen as a function (right ?)

type operator-()
type& operator-()


1. Don't pass [non-const] references of pointers to functions.
2. If your function *must* modify one of it's arguments,
pass a reference or a pointer to the object then
return a reference or a pointer to that object
*instead of implementing a void function"
so that you can use the function in expressions.

You compiler will *not* be able to resolve an invocation
of operator-() to either of the two candidates above.
You must implement one or the other -- not both.

You will need to explain the [overloaded] meaning
of operator- before we can help you decide
whether to return a value or a reference
but built-in operator- *always* returns a value.
Jul 23 '05 #3
Ahmed MOHAMED ALI wrote:
Passing parameters by reference is not only used for modification purpose.
It's used also to avoid copying for performance issue.
You return an argument by reference for the same reason.


This is a common mistake.
Return by value does *not* require a copy.

Suppose that you have huge objects of type

class Huge { };

The typical optimizing C++ compiler will emit
essentially the same code for a function:

Huge f(void) {
Hugh return_value;
// modify return_value
return return_value;
}

which returns a Huge object by value
as it does for an equivalent function:

Hugh& f(Hugh& return_value) {
// modify return_value
return return_value;
}

which returns a reference to a Huge object.
Jul 23 '05 #4
wo*********@yahoo.com wrote:
Now my question is, why would you prefer to let a function return a
reference or a pointer ???
Consider the following with operator overloading. I suppose the
operator can be seen as a function (right ?)

type operator - ()
type& operator - ()


I think your questions can be best answered by the FAQ. See sections 8
and 13.9.

Kristo

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
6
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
4
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
8
by: optimistx | last post by:
In excellent YAHOO user interface programs I see often extra parenthesis like this (foo=function(){ alert('I am foo'); })(); instead of bar=function(){
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.