Datasheet
Chapter 1: A Quick Introduction to Programming
22
Finally, notice how, in the second to last line, the function name PromptUserName is treated as if it were
a variable. When you use functions (as opposed to subprocedures, which do not return a value), this is
how you give the function its return value. In a sense, the function name itself is a variable within the
procedure.
Here is some code that uses the
PromptUserName function.
Dim Greeting
Dim VisitorName
VisitorName = PromptUserName
If VisitorName <> “” Then
Greeting = “Goodbye, “ & VisitorName & “. Nice to have met you.”
Else
Greeting = “I’m glad to have met you, but I wish I knew your name.”
End If
MsgBox Greeting
If you are using Windows Script Host for this code, bear in mind that this code and the PromptUserName
function itself must be in the same
.vbs script file.
Dim PartingGreeting
Dim VisitorName
VisitorName = PromptUserName
If VisitorName <> “” Then
PartingGreeting = “Goodbye, “ & VisitorName & “. Nice to have met you.”
Else
PartingGreeting = “I’m glad to have met you, but I wish I knew your name.”
End If
MsgBox PartingGreeting
Function PromptUserName
‘ This Function prompts the user for his or her name.
‘ It incorporates various greetings depending on input by the user.
Dim YourName
Dim Greeting
YourName = InputBox(“Hello! What is your name?”)
If YourName = “” Then
Greeting = “OK. You don’t want to tell me your name.”
ElseIf YourName = “abc” Then
Greeting = “That’s not a real name.”
ElseIf YourName = “xxx” Then
Greeting = “That’s not a real name.”
c01.indd 22c01.indd 22 8/27/07 7:45:25 PM8/27/07 7:45:25 PM