Syntax
Function FunctionName[$][(ArgumentList)]
Series of instructions to determine a value
FunctionName[$] = value
End Function
Remarks
Defines a function — a series of instructions that returns a single value. To return
a string value, the function name must end with a dollar sign ($). Note that unlike the names of built-in WordBasic functions, the names of user-defined functions that do not specify ArgumentList do not end with empty parentheses; if you include empty parentheses, an error will occur.
ArgumentList is a list of variables, separated by commas, that are passed to the function by the statement calling the function. String variables must end with a dollar sign. ArgumentList cannot include values; constants should be declared as variables and passed to the function through variable names.
For more information about creating functions, see Chapter 4, "Advanced WordBasic," in Part 1, "Learning WordBasic."
Example
This macro prompts the user to type a number of degrees Fahrenheit, which is passed to the ConvertTemp() function through the variable fahrenheit. The function converts fahrenheit to degrees Celsius, and then the main subroutine displays this value in a message box.
Sub MAIN On Error Resume Next tmp$ = InputBox$("Type a Fahrenheit temperature:") fahrenheit = Val(tmp$) celsius = ConvertTemp(fahrenheit) MsgBox tmp$ + " Fahrenheit =" + Str$(celsius) + " Celsius" End Sub Function ConvertTemp(fahrenheit) tmp = fahrenheit tmp = ((tmp - 32) * 5) / 9 tmp = Int(tmp) ConvertTemp = tmp End Function
See Also
Sub¼End Sub