Pass by reference or value?

General discussion about C/C++

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Pass by reference or value?

Postby DannyBoy » Thu Jan 27, 2011 10:25 am

Hi all:

I have a small low level function that I use to convert between Celcius and Kelvin. First, is using such a function wasteful (in terms of runtime) given that it essentially just adds 273.15 to the passed value? Next, which of the following implementations is better?

Code: Select all
inline void C2K(double *T){*T+=273.15;}//Convert Celcius to Kelvin                                                                                                                             

double SomeFunction(double T,double S){                                                                                                                           
  C2K(&T);//Convert to Kelvins                                                                                                                                                                         
  return exp(2.18867-2275.036/T-1.468591*log(T)+(-0.138681-9.33291/T)*sqrt(S)+0.0726483*S-0.00574938*pow(S,1.5));
}


or

Code: Select all
inline double C2K(double T){return T+=273.15;}//Convert Celcius to Kelvin                                                                                                                             
double SomeFunction(double T,double S){                                                                                                                           
  T=C2K(T);//Convert to Kelvins                                                                                                                                                                         
  return exp(2.18867-2275.036/T-1.468591*log(T)+(-0.138681-9.33291/T)*sqrt(S)+0.0726483*S-0.00574938*pow(S,1.5));
}


Thought, comments, et cetera, would be appreciated!
User avatar
DannyBoy
 
Posts: 1160
Joined: Fri Feb 13, 2004 12:56 pm
Location: In the Billiard Room with the Lead Pipe

Re: Pass by reference or value?

Postby Wizard » Fri Jan 28, 2011 9:03 am

By making it inline, the overhead of it being a function call disappears. The only practical difference between the function call and simply adding 273.15 is that "C2K" is a little more obvious what it's doing vs adding some number that may not be obvious to the next person to read your code. So yes, it may be simple, but it's still a good idea to make it a function in this case.
As for the pointer vs return, I think a return is much more portable and makes more sense in this case. This way you can go "double T = C2K(100);" and or use other constants without worry. Especially the case if you have "objectA.getTemperature();" or something like that, it's easier to handle with a return. Maybe that's not your use case, but no point in limiting yourself early on when you can make it a return for the same cost.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Re: Pass by reference or value?

Postby DannyBoy » Mon Jan 31, 2011 6:03 pm

Thanks for the reply, Wiz! One more question: if I use inline inappropriately (e.g., for a large function) will it be detrimental to my runtime or will the compiler just refuse to inline the function? (I seem to remember someone mentioning that inline is merely a suggestion to the compiler, but I may well be misremembering!)
User avatar
DannyBoy
 
Posts: 1160
Joined: Fri Feb 13, 2004 12:56 pm
Location: In the Billiard Room with the Lead Pipe

Re: Pass by reference or value?

Postby Wizard » Tue Feb 01, 2011 8:56 am

The general rule of thumb is that modern compilers are smarter than you. ;)
Inline is just a hint. Long ago, in a galaxy far far away, inline might have always made the compiler inline if you told it to, but new compilers optimize themselves. They'll inline functions that you don't say inline, they'll ignore inline for functions that don't make sense. They might put more weight into it if the developer thinks it should be inlined, but it's not guaranteed.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Re: Pass by reference or value?

Postby DannyBoy » Tue Feb 01, 2011 9:04 am

Every day's a school day! :D
Much appreciated. /DB
User avatar
DannyBoy
 
Posts: 1160
Joined: Fri Feb 13, 2004 12:56 pm
Location: In the Billiard Room with the Lead Pipe


Return to General

Who is online

Users browsing this forum: Google [Bot] and 3 guests