472,331 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 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 4196
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...
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...
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...
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...
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...
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...
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 ...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.