Parameter Passing

Association of actual and formal parameters upon function call. Six relatively common methods are: CallByValue (or "pass-by-value")

Simplest; actuals evaluated, r-values passed. Used e.g. in C, Pascal. CallByReference (or "PassByReference")

Caller passes called procedure a pointer to storage location of each actual parameter. Optionally used in Pascal (var parameters). Arrays are usually passed by reference. CallByValueResult (or "copy-restore", "copy-in copy-out")

Hybrid of call-by-value and call-by-reference. CallByName

Historically introduced by the "copy-rule" of Algol: CallByNeed

See LazyEvaluation. [[need description here]]

(Based on discussion in RedDragonBook, sec 7.5)

AnswerMe: Can some one please explain the differences between macro-expansion, call-by-name and call-by-thunk? From the list at the top I'd gather that call-by-name is different from macro-expansion, but the explanation below that mixes them. And one the first words on 'CallByName' is 'thunk', and the explanations given all sound alike.
Following coming from ParameterPassingModes?, which is being merged in:

How one passes parameters/arguments from an outer scope to another scope (a function, macro, etc.) is a key issue in language design, with far-reaching consequences. Several different types are in use. It is useful to distinguish semantics from implementation. (Note that issues like VariableCapture? and ScopingRules are not handled here; only those values passed explicitly as parameters).

Implementation techniques: How languages actually implement parameter passing.

Semantic modes. For some languages, such as CeeLanguage and CeePlusPlus, the implementation and the semantics are equivalent; in C you always get CallByValue, can simulate CallByReference with pointers, etc. For other languages, "higher level" semantics that define the observable behavior of parameter passing are what is important; implementations have freedom to implement how they like.

Finally, one other important consideration is when arguments are evaluated.


See also AlternativesToPassByReference, ReturnByReference, JavaPassesByValue

View edit of June 29, 2005 or FindPage with title or text search