BrainDump
  Home arrow BrainDump arrow VBScript: Functions and Loops
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

VBScript: Functions and Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-12-17

    Table of Contents:
  • VBScript: Functions and Loops
  • Sub Procedures
  • Loop Flakes...They're Great!
  • The For Each...Next Loop
  • Do Loop
  • Do Until

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More BrainDump Articles
    More By James Payne


     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT