• Advertisement
Sign in to follow this  

passing variables/objects to functions

This topic is 3609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is probobly a dumb question, but which is more efficent, passing a variable/object by value, or passing it by const reference? the reason i ask is because i remember reading something a while back saying that when you pass by value, the function creates of a copy of it, and i'm just wondering if that would take some overhead. i'm just trying to get into good habits. also, is there any disadvantage to passing by const reference? thanks

Share this post


Link to post
Share on other sites
Advertisement
Depends. If in doubt profile it both ways and check. In general, pass primitive types and enums by value and pass class types by reference.

Share this post


Link to post
Share on other sites
This is in VB I assume?

Depending on your IDE really.

In VB6 I speed tested and found ByVal faster than ByRef - however ByRef is ideally the 'faster' one.

The only disadvantage I see passing it ByRef is that if you wish to edit the arguements value but not change it outside of the routine you would just have to declare a new variable and transfer the contents over to that.

Share this post


Link to post
Share on other sites
Quote:
Original post by godsenddeath
the reason i ask is because i remember reading something a while back saying that when you pass by value, the function creates of a copy of it, and i'm just wondering if that would take some overhead. i'm just trying to get into good habits. also, is there any disadvantage to passing by const reference?

In C++, when passing an object (class/struct) by value, it's copy-constructor will be called (which can have significant overhead). So with objects you almost always want to use pass-by-const-reference instead.
You'll usually only want to use pass-by-value for primitive types (int/float/pointer values/etc), and pass-by-(non-const-)reference when you want to modify the original inputted variable.

Share this post


Link to post
Share on other sites
I am not strongly grapsed with C++ - however anyways - I assume if you would want to pass an object, you'd pass its pointer value (const reference) so you can edit it through the routine right?

I'm guessing is that if you don't pass it as a reference - the object would just be re-created within the routine again(Think of it like in 'virtual space' - if you pass the refence it'll only be a 32-bit value where as if you re-create the object you take up more of that virtual space) - so I am assuming it would be just a tad fraction slower than passing it as a pointer.

Sorry I am not entirely sure myself but I would like to see if my idea is right atleast!

*edit* Well above post just clarified it all thanks :D

Share this post


Link to post
Share on other sites
I'm not a really advanced programmer myself, but I would say pass by value unless the variable is something large like a struct or class. I would even pass by value if it was a small struct like a simple rectangle composed of 4 variables.

For smaller things, the amount of time taken passing it into functions over and over is small enough to not be troubled about. For larger things, it still probably isn't worth bothering about, unless it's a huge project, but the extra hassle of passing it by pointer or a reference is so small, that it seems pointless not to do that extra work.

[Edit:] Unless ofcourse, you are wanting to edit the value in the function, without putting the return value back into the object passed, in which case you have to use a pointer or reference it.

Share this post


Link to post
Share on other sites
Basically there is a little bit of overhead in passing by reference as well. so for small types by value or by reference is about the same with by reference being a little slower. For large objects copies are expensive so pass by reference is better.

Share this post


Link to post
Share on other sites
Sounds like deja vu

Long story short. It depends on many factors.

Rule of a thumb - if it fits into a register, pass it by value. So, 32-bits.

If you pass by reference, you're effectively passing a pointer, which needs to be copied as well, and dereferenced.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement