bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Creating Macros > Subroutines and Functions > Understanding WordBasic Functions

Understanding WordBasic Functions

Summary: When you use functions, you define program routines that return values you can use elsewhere in your program. (This tip works with Microsoft Word 6, and Word 95.)

You already know that you can use subroutines in your macros. WordBasic allows you to define functions that can be used in your macros. The difference between functions and subroutines is that functions can return values, whereas subroutines cannot. Consider the following macro:

Sub MAIN
    TooMany = TestFunc
    If TooMany Then Print "Too many pages"
End Sub

Function TestFunc
    TestFunc = 0
    If SelInfo(4) > 10 Then TestFunc = -1
End Function

This function returns either the value 0 (False) or -1 (True), depending on a test it performs. The main program then acts upon the value returned. Notice that the function name can appear on the right side of an equal sign. This makes functions very powerful and an important part of any program. Within the function the result is assigned to TestFunc, which is the name of the function itself; this is the value returned by the function.

As with subroutines, you can also pass parameters to your functions. This is illustrated in the following macro:

Sub MAIN
    A = 12.3456
    Print 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 WordBasic 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 #139 applies to Microsoft Word versions: 6 | 95


Find and Replace Almost Anything! An invaluable resource for learning how to harness the full power of Word's search and replace capabilities. You'll discover everything you need in order to master all the intricacies of finding and replacing elements of your document, including the super-powerful "wildcard searches" available in Word.
 
Check out WordTips: Find and Replace today!

Helpful Links

Ask a Word Question
Make a Comment

Tips.Net Home
Tips.Net Store

WordTips FAQ
WordTips Premium

Learn Access Now

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

Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)