void aFunction(int &myInt)
{
myInt = anotherInt; //This is sometimes more effecient than having a return value at all.
}
ehhhh. I wouldn't advise code like this.
I actually see a lot of people in the newbie board passing int& as parameters. I suspect this is because people spout out "passing by reference is more efficient". Granted you didn't say that here (or at least you qualified it with "sometimes"), but I felt like chiming in anyway.
Function formats like this can get in the way of compiler optimizations and make your code harder to read and maintain. Plus they make the function harder to use because you can't put it as part of another statement:
class MyClass
{
public:
int get() { return anotherInt; }
void fakeget(int& v) { v = anotherInt; }
protected:
int anotherInt;
};
void func()
{
MyClass a, b;
int foo = a.get() + b.get(); // easy 1 liner, AND [likely] more efficient
int bar, bar2;
a.fakeget(bar);
b.fakeget(bar2);
bar += bar2; // not as easy 4 liner, [likely] less efficient
}
Now if you're talking about large objects like classes, where copying the object is expensive -- then yeah, passing by reference might be the way to go. Or, of course, if you need to "get" more than one variable, then passing by reference is pretty much necessary.