Select the product you need help with
WD: Passing Parameters "By Reference" and "By Value"Article ID: 94374 - View products that this article applies to. This article was previously published under Q94374 On This PageSUMMARY
By default, Word passes parameters to subroutines or functions "by
reference." To pass a variable "by value," enclose the variable in
parentheses when you make the call to the subroutine or function.
MORE INFORMATIONWord 97 for Windows or Word 98 Macintosh EditionFor more information about passing a variable "by value," while in the Visual Basic Editor, click the Office Assistant, type Passing Arguments Efficiently click Search, and then click to view "Passing Arguments Efficiently."NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Visual Basic Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base: 120802
(http://support.microsoft.com/kb/120802/EN-US/
)
Office: How to Add/Remove a Single Office
Program or Component
Passing a Variable "By Reference"When you pass a variable "by reference," the subroutine or function can change the value of that variable. This change affects the value in the subroutine as well as the variable in the calling routine.Consider the following example: The variable a$ is passed to the subroutine TestSub where the variable is referenced by b$. Since the value of a$ is being passed "by reference," a$ and b$ actually point to, or share, the same data information. When b$ is assigned a new value, a$ will change as well. Passing a Variable "By Value"When a variable is passed "by value," the subroutine or function gets a copy of the information. If the subroutine or function assigns a new value to the variable, the change affects only the copy of the information and not the original that was passed to the subroutine or function.Consider the following example: NOTE: There is only one case in which the macro passes parameters "by reference" when they are enclosed in parentheses. If you run a subroutine and precede the subroutine name with the WordBasic Call statement, the macro passes all parameters "by reference," even though the parameters are enclosed in parentheses. For example, if you call the TestSub subroutine in the above macro with the following syntax, the message box displays "By Reference!", even though you enclosed the variable in parentheses: Examples: Passing Values "By Reference" And "By Value"Consider the following function:
a$ b$
---------------------------------------------
z = TestFunct(a$, b$) by ref by ref
z = TestFunct((a$), (b$)) by val by val
z = TestFunct((a$), b$) by val by ref
z = TestFunct(a$, (b$)) by ref by val
How WordBasic Differs from Other Microsoft Basic LanguagesWordBasic passes variables "by reference" and "by value" in a different manner than most Microsoft Basic programming language applications (such as Microsoft Visual Basic, Microsoft QuickBasic, and Microsoft Professional Basic).In WordBasic, the caller is responsible for using parentheses to determine whether information passes "by reference" or "by value." By contrast, other Microsoft Basic languages make this distinction in subroutines and functions using the BYVAL keyword in the parameter list of the declaration. By default, other Microsoft Basic languages pass variables "by reference" (this is the same as WordBasic). If you precede the variable with BYVAL in the subroutine or function declaration, the values pass "by value." Consider the following Visual Basic subroutine: PropertiesArticle ID: 94374 - Last Review: January 18, 2007 - Revision: 2.5 APPLIES TO
|



Back to the top








