
Tips.Net > WordTips Home > Creating Macros > Subroutines and Functions > Passing Parameters to Functions
Summary: Functions are a programming structure that allows you to calculate and return a result. They are extremely powerful, and are therefore commonly used in programming. This tip explains how you can enhance your functions by passing information to them that can be used by the function to derive the results you need. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
When you create your own functions, it is often helpful to pass parameters to the function. These parameters can be used either as data that you want the function to act upon, or as settings used to control how the function does its work. How you pass parameters to functions is illustrated in the following macros:
Sub Macro1()
A = 12.3456
Status.Bar = A & " " & Round(A)
End Sub
Function Round(X)
Round = Int(X + 0.5)
End Function
This simple macro defines a number, and then prints it and the result of passing the number to the Round function; the output is 12.3456 and 12. Notice that the parameter should be passed to the function within parentheses. Also notice that the function does not use the same variable name as it was passed. This is because VBA reassigns the value of X (what the function needs) so it matches the value of A (what the program is passing to the function). The important thing to remember in passing parameters to functions is that your program must pass the same number of parameters as the function expects, and the parameters must be of matching types and in the proper order.
Tip #1536 applies to Microsoft Word versions: 97 2000 2002 2003
Ultimate Library! An amazing resource that brings together, in one place, the collected knowledge of everything ever published in WordTips. The library combines two powerful elements to make you more productive: solutions and convenience. Here's where you get your own copy of everything ever published in WordTips.
Check out WordTips Ten-Year Library today!
Uncover how you can master the full potential of printing your documents. Everything you wanted to know about printing and printers, using Word. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips