
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
Take Control! Master the real power behind Word! Successfully master the secrets of powerful formatting and create documents that stand out from the rest. Best of all, you can create documents that are easy to maintain and quick to change.
Check out Word 2007 Styles and Templates today!
Learn to use styles and you discover the real power behind Word. Fast, easy, consistent document formatting and updating is within your grasp. (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