The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

should caller or func defn control by ref or by val?

If you have a language that allows passing variables by value or by reference, I think you can have 3 ways of controlling it:

1. caller decides, e.g.

function f(a)

f(ref a)

2. function definition decides, e.g.

function f(ref a)


3. function definition sets default, but caller can explicitly choose, e.g.

function f(ref a)

f(val a)

What are the pros and cons? Any good links on this subject?
Wednesday, September 27, 2006
Ultimately the function has control anyway.  The caller can't make a byval parameter into a byref.  If your wrote every function with every parameter byref you'd also have unnecesary overhead.

That goes double (or worse) for any remote function call, where marshalling everything both ways is absurd.

I'd have to rate the notion "ultra lame" myself but I'd be interested in arguments in favor of it.
Slim Simi
Wednesday, September 27, 2006
Good points. Yes, a caller can't make a byval into a byref if the function doesn't modify it anyway. But in theory a particular function could be designed to modify a certain argument if you cared to pass it by reference. Too confusing to be useful though.
Wednesday, September 27, 2006
Sure, there'll usually be a cleaner way to design an API such that that kind of thing is redundant, but out here in the real world we don't always get to use well-designed APIs...

At any rate, I believe VB used to provide something like this -- wasn't there a way to tell it to make an implicit copy of a variable, so you could pass a variable to a function that took a ByRef parameter, without your copy of the variable being changed?

I don't think I ever used it, but it wouldn't have existed if there hadn't been someone who thought it was useful.
Wednesday, September 27, 2006
Pascal allowed the definition of the PROCEDURE/FUNCTION to specify if what was passed in was a 'VAR' parameter or not.  Caller's didn't have to care, which was convenient.

If it WAS 'VAR', then it was passed-by-reference.  If it was not VAR, then it was passed-by-copy and mods made to it in the subroutine would not be passed back to the caller.

Now, in 'C', if you wanted to change the value of a parameter and have it be passed back, you had to specify this BOTH in the definition of the call (as a 'pointer-to') and in every call (as an 'address-of', unless you already HAD a 'pointer-to').  Personally I think this is a major pain.

Personally, I find it makes the code much easier to maintain if the 'definition' alone controls byref or byval.  That's because the definition is defined ONCE, always, where it may be CALLED from anywhere.  This centralizes the decision, and allows it to be changed
Friday, September 29, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz