Microsoft KB Archive/94374

= WD: Passing Parameters &quot;By Reference&quot; and &quot;By Value&quot; =

Article ID: 94374

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Word 1.0 Standard Edition
 * Microsoft Word 1.1 Standard Edition
 * Microsoft Word 1.1 Standard Edition
 * Microsoft Word 2.0 Standard Edition
 * Microsoft Word 2.0a
 * Microsoft Office Word 2003
 * Microsoft Word 2.0c
 * Microsoft Word 6.0 Standard Edition
 * Microsoft Word 6.0a
 * Microsoft Word 6.0c
 * Microsoft Word 95 Standard Edition
 * Microsoft Word 95a
 * Microsoft Word 97 Standard Edition
 * Microsoft Word 6.0 for Macintosh
 * Microsoft Word 6.01 for Macintosh
 * Microsoft Word 6.01 for Macintosh
 * Microsoft Word 98 for Macintosh

-



This article was previously published under Q94374



SUMMARY
By default, Word passes parameters to subroutines or functions &quot;by reference.&quot; To pass a variable &quot;by value,&quot; enclose the variable in parentheses when you make the call to the subroutine or function.



Word 97 for Windows or Word 98 Macintosh Edition
For more information about passing a variable &quot;by value,&quot; while in the Visual Basic Editor, click the Office Assistant, type Passing Arguments Efficiently click Search, and then click to view &quot;Passing Arguments Efficiently.&quot;

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 Office: How to Add/Remove a Single Office Program or Component

Passing a Variable &quot;By Reference&quot;
When you pass a variable &quot;by reference,&quot; 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: Sub MAIN a$ = &quot;By Value!&quot; TestSub a$     MsgBox a$   End Sub

Sub TestSub(b$) b$ = &quot;By Reference!&quot; End Sub When you run this macro, the message box displays &quot;By Reference!&quot;.

The variable a$ is passed to the subroutine TestSub where the variable is referenced by b$. Since the value of a$ is being passed &quot;by reference,&quot; 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 &quot;By Value&quot;
When a variable is passed &quot;by value,&quot; 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: Sub MAIN a$ = &quot;By Value!&quot; TestSub (a$) MsgBox a$  End Sub

Sub TestSub(b$) b$ = &quot;By Reference!&quot; End Sub The only difference from the previous example is that parentheses appear around the argument a$, which is being passed to the subroutine TestSub. However, when the macro runs, the message box displays &quot;By Value!&quot;. In TestSub, b$ gets a copy of what was in a$. a$ and b$ do not share the same value; instead they have identical copies of the data.

NOTE: There is only one case in which the macro passes parameters &quot;by reference&quot; 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 &quot;by reference,&quot; 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 &quot;By Reference!&quot;, even though you enclosed the variable in parentheses: Call TestSub(a$)

Examples: Passing Values &quot;By Reference&quot; And &quot;By Value&quot;
Consider the following function: Function TestFunct(x$, y$) x$ = &quot;New X Value!&quot; y$ = &quot;New Y Value!&quot; End Function Below is a table of calls to the TestFunct function that indicates whether the macro passes the values &quot;by reference&quot; or &quot;by value.&quot;  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 Languages
WordBasic passes variables &quot;by reference&quot; and &quot;by value&quot; 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 &quot;by reference&quot; or &quot;by value.&quot; 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 &quot;by reference&quot; (this is the same as WordBasic). If you precede the variable with BYVAL in the subroutine or function declaration, the values pass &quot;by value.&quot;

Consider the following Visual Basic subroutine: Sub TestSub(a$, BYVAL b$) a$ = &quot;New a$ value!&quot; b$ = &quot;New b$ value!&quot; End Sub If you use the following syntax to call this subroutine, the first message box displays &quot;Old a$&quot;, and the second displays &quot;New b$ value!&quot;: a$ = &quot;Old a$&quot; b$ = &quot;Old b$&quot; TestSub a$, b$  MsgBox a$   MsgBox b$

Additional query words: winword2 VB QB winword 7.0 word95 Word98 macword word7 word6 word8 word97 8.0 b_vbasic b_vbmsdos b_quickbasic passing variables userforms

Keywords: kbinfo kbmacro kbprogramming KB94374

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.