VBScript: Functions and Loops
(Page 1 of 6 )
In our last tutorial, we discussed statements and multidimensional arrays. In this article we are going to pick up on functions and work our way through to loops. It's a lot to cover in a short amount of time, so buckle up and put on your helmet. No not your special helmet...the other one.
Functions
My old boss used to say to me all the time, "James...I don't know how you live." I won't say why he said that. Just know that he did. Granted the guy is worth like $100 million, so in all honesty, I REALLY don't know how he lives either, though I would sure like to. But I think what he was really saying was: I don't know how you function. So let's talk about functions.
Aside from the built-in functions in VBScript (such as document.write), you can also create your own functions. In VBScript (and every programming language really), functions are used to save programmers from having to write the same bit of code over and over again. Imagine a video game like Mike Tyson's Punch Out. Every time you press the "b" button, Little Mac throws a left-handed punch to the stomach. The pressing of that "b" button calls or triggers the function. That, in turn, tells the program to have Little Mac throw that punch. Now imagine that the programmer had to program the same code over and over every time that punch was thrown. It would result in a billion lines of code and a very angry programmer.
But with functions, he can program it once and then simply call it when the user presses the "b" button.
Our First Function
So let's create a simple function to write some things to the screen:
<html>
<head>
<script type="text/vbscript">
Function myFunction()
document.write("I hold these truths to be self-evident <br />")
End function
</script>
</head>
<body>
<script type="text/vbscript">
myFunction
myFunction
myFunction
myFunction
myFunction
myFunction
myFunction
myFunction
myFunction
myFunction
</script>
</body>
</html>
The above code creates a function that, once called, will print out some text to the browser. We then call the function, not once, but ten times. Each time the function is called, it will print out our text. Here is the result:
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
I hold these truths to be self-evident
You may have noticed that we write a function like this: Function myFunction(). The parentheses can be used for holding an argument. In the following example we will create a function to do some math:
<html>
<head>
<script type="text/vbscript">
Function calc(monkey, soup)
calc = monkey + soup
End function
Dim answer
answer = calc(20,30)
document.write(answer)
</script>
</head>
<body>
</body>
</html>
This will create a function that, once called, will add the values in the variables and print the result to the screen:
50
Note that the values get added to our variables in this line of code: answer = calc(20,30). And that the variables are the arguments in parentheses when we create our function: Function calc(monkey, soup).
You can also use the function in a pop-up box, like so:
<html>
<head>
<script type="text/vbscript">
Function calc(monkey, soup)
calc = monkey + soup
End function
Dim answer
answer = calc(20,30)
msg.box "20 + 30 equals " & answer
</script>
</head>
<body>
</body>
</html>
This will create a pop-up box with the result of the equation: 50.
Next: Sub Procedures >>
More BrainDump Articles
More By James Payne