Vous êtes sur la page 1sur 3

Dim MyDate, MyWeekDay

MyDate = Date ' Assign a date.


MsgBox MyDate
MyWeekDay = Weekday(MyDate) ' MyWeekDay contains 6 because MyDate represents a
Friday.
MsgBox MyWeekDay
When you define a function like this:
Function MyFunction(strArgument)
End Function
You're declaring a function named "MyFunction" which accepts a single
input argument, named "strArgument." Inside the function, you DO NOT NEED
to declare strArgument: While it works a lot like a variable, it's not;
it's an ARGUMENT, and it's "declared" as part of the function.
When you call the function:
WScript.Echo MyFunction("hello")
Notice that you can pass a literal value to the argument. Or, you can
pass the contents of a variable:
Dim strInput
strInput = "hello"
WScript.Echo MyFunction(strInput)
Whatever you pass to the function's argument, it's value will be contained
WITHIN the argument. So, a simple function might just output whatever
you've passed in:
Function MyFunction(strArgument)
WScript.Echo strArgument
End Function
When your function has a value to return, do so by assigning that value
to the function name, as the last line of the function's code:
Function MyFunction(strArgument)
Dim bolResult
If strArgument = "uncle" Then
bolResult = True
Else
bolResult = False
End If
MyFunction = bolResult
End Function
So, to make this into a full-fledged example, here's some code and the
function it calls:
Dim strInput
strInput = InputBox("Type the secret word!")
If GotSecret(strInput) Then
MsgBox "That's right!"
Else
MsgBox "Nope!"
End If

Function GotSecret(strArgument)
Dim bolResult
If strArgument = "uncle" Then
bolResult = True
Else
bolResult = False
End If
GotSecret = bolResult
End Function
The InputBox function is used to get a string value from the user, and
that value is stored in strInput. Then, an If...Then block calls the
GotSecret() function, passing the contents of strInput as the input to the
function. The function returns either a True or False value, which determines
whether the message "That's right!" or "Nope!" is displayed.
You try it:
FUNCTION AND SUB EXERCISE
=========================
1. Declare a variable named intTest
2. Assign the value 10 to the variable intTest
3. Create a function named DoubleUp, which accepts a single input argument
The function should take its input, double it, and return the result
as the result of the function
4. Have your script pass intTest to the DoubleUp function, and then
echo the result of the function to the screen.
** Try to accomplish this exercise in exactly 6 lines of code
(not including Option Explicit, if you use that).

FUNCTION AND SUB SOLUTION


(NO PEEKING)
=========================
Dim intTest
intTest = 10
WScript.Echo DoubleUp(intTest)
Function DoubleUp(intValue)
DoubleUp = intValue * 2
End Function