LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2010, 09:01 AM   #1
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Rep: Reputation: 18
The & operator on functions


Hi,

I was reading some functions and some examples, but I couldn't understand what the & operator exactly do.

For example:

Code:
string& doSomething1(string& myStr) {
  //do some stuff
  return myStr;
}
Question 1 - Could someone explain to me what means the & operator on above function?
(I think it's something like 'pass by value', but I'm not sure)

Question 2 - Is there any difference with:
Code:
string doSomething2(string myStr) {
  //do some stuff
  return myStr;
}
??


I was trying to look for this operator, but with google I was finding just examples about get pointer value. Someone knows any good link to solve doubts like this?

thanks in advance
 
Old 07-19-2010, 09:09 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
When you ask a question like that in this forum, please mention the programming language. This time we can tell it is C++, but this forum is used for many languages and often we can't tell.

Quote:
Originally Posted by Lobinho View Post
Code:
string& doSomething1(string& myStr) {
  //do some stuff
  return myStr;
}
The & means the result of the function is returned by reference. The & means the parameter myStr is passed by reference.
Quote:
(I think it's something like 'pass by value', but I'm not sure)
No, almost the opposite. The default (without either &) is pass and return by value. The & makes it by reference.

Quote:
Question 2 - Is there any difference with:
Code:
string doSomething2(string myStr) {
  //do some stuff
  return myStr;
}
That means by value.

When passing by value, the string is copied when passed. When returning by value, the string is copied when returned.

When passed by reference, any modifications made to the string inside the function affect the original string that was passed. But if passed by value, such modifications only affect the copy.

When something is returned by reference, the function must be coded carefully to make sure the returned object still exists after the function exits. So you could mix the two versions you posted to make a function with pass by reference and return by value, but if you tried pass by value and return by reference you would need bigger changes, because the copy made in the pass by value doesn't last long enough to be returned by reference.

Quote:
I was trying to look for this operator, but with google I was finding just examples about get pointer value. Someone knows any good link to solve doubts like this?
& in C and C++ is normally an "address of" operator, which converts an "L Value" into a pointer to that L Value.

But when used in a declaration in C++, & means that the thing being declared is a reference.

I don't have any good links. But you could look up the word "reference" in any C++ tutorial or documentation and get more details.

Last edited by johnsfine; 07-19-2010 at 09:20 AM.
 
1 members found this post helpful.
Old 07-19-2010, 09:12 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,623

Rep: Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942
Firstly, before you go asking about functions and operators and such, it is generally nice to let people know what language you are talking about??
It could make a very big difference to what is the correct answer.

At a guess based on some of the information provided I will say you are looking at C or C++. So my advice is to look up those languages
and specifically search for 'pass by reference' and 'pass by value'
 
Old 07-19-2010, 09:12 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
What language? (It looks like it might be C)

In C, "&" is either:

address operator (goes in front of a variable)
OR:
bitwise AND

PS: Many better answers while I was reading my C manual....

Last edited by pixellany; 07-19-2010 at 09:13 AM.
 
Old 07-19-2010, 11:53 AM   #5
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Thanks all for the replies.

johnsfine, your explanation was very didactic, thanks again.
 
Old 07-19-2010, 12:02 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by Lobinho View Post
your explanation was very didactic
That sent me over to dictionary.com to discover that the meaning of "didactic" that I knew is second on the list of meanings. A meaning that I didn't know, that is more consistent with your "thanks", is first.

The meaning I knew (only meaning for any of the previous uses of "didactic" I had seen) was:

inclined to teach or lecture others too much

I guess that one might also be true of my posts at LQ

Last edited by johnsfine; 07-20-2010 at 05:35 AM.
 
Old 07-19-2010, 07:32 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,623

Rep: Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942Reputation: 2942
Too clever a word for me at this time of the morning .. sheesh
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] TCL AND operator (&) on a binary value jlinkels Programming 16 05-09-2010 07:27 AM
How to Write & Call Functions Within Shell Scripts senthilmuthiah Linux - Newbie 2 03-27-2009 06:36 PM
A question about the & operator in C trist007 Programming 3 10-03-2008 05:37 PM
Bash scripting and && operator Dark_Helmet Programming 16 09-18-2005 09:57 PM
how to pass argv[] & argc to functions? skie_knite007 Programming 2 05-13-2005 11:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration