Results 1 to 5 of 5

Thread: How to create Functions in VBScript?

  1. #1
    Join Date
    Jul 2006
    Posts
    339

    How to create Functions in VBScript?

    Hi friends,
    I am new to the scripting. I have done C, C++ and VB. But I am totally new for the scripting part. I have started recently with the VBScript. Can anyone explain me how to create functions in VBScript? Because I know that using the function will definitely decrease my coding lines. So I am very eager to create the functions. Please help me by providing some sample of coding for creating the function in VBScript.!!

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to create Functions in VBScript?

    You are absolutely right, that using the function will reduce your coding lines in the program. When you write the exact code, functions are also useful to increase code readability. all the programs that you write will benefit from functions. Whether you use the pre-made functions like document.write() or make your own it is a necessity for any programmer to have a basic understanding of how functions work in VBScript. So it will be better to use the functions in VBScript.

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    Re: How to create Functions in VBScript?

    Since you are new in VBScript, lets use some simple function for an example. The function myAdd will take two numbers and print out the result. The following is the code in VBScript for that example :
    Code:
    <script type="text/vbscript">
    Function myAdd(x,y)
        myAdd = x + y
    End Function
    'Let's use our first function!
    Dim result
    result = myAdd(12,18)
    document.write(result)
    </script>
    After using the above code for the myadd function, you will get the following output :
    Code:
    30

  4. #4
    Join Date
    Mar 2008
    Posts
    349

    Re: How to create Functions in VBScript?

    The Function and End Function keywords defines the function in VBScript. The following is the method used for creating VBScript functions :
    1. Start by using the Function keyword. by using this the browser knows that the function is defined.
    2. Then provide the name for the function.
    3. Use the opening and closing brackets after writing the name.
    4. Add the names of any arguments that the function requires.
    5. Write the code that makes up the function, which should be started on the new line.
    6. Lastly finish with the End Function.

    The following example will describe it :
    Code:
    Function add(number1,number2)
      add = number1 + number2
    End Function

  5. #5
    Join Date
    Aug 2006
    Posts
    227

    Re: How to create Functions in VBScript?

    The following example demonstrates the creating and calling a VBScript function.
    Code:
    <script type="text/vbscript">
    Function add(number1,number2)
      add = number1 + number2
    End Function
    Dim total
    total = add(15,10)
    document.write(total)
    </script>
    Using the above code, the following output will be produced:
    Code:
    25
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

Similar Threads

  1. How to create factorial of numbers using sub procedure in vbscript
    By Gurseerat in forum Software Development
    Replies: 1
    Last Post: 04-02-2012, 12:21 PM
  2. How to create ZIP Files from Command-Line via VBScript?
    By FB in forum Windows Server Help
    Replies: 10
    Last Post: 22-12-2011, 12:18 AM
  3. How to create procedures in VBScript
    By Betelgeuse in forum Software Development
    Replies: 4
    Last Post: 30-11-2010, 01:45 AM
  4. how to create automatic self extracting zip file in vbscript
    By vivekmohan in forum Software Development
    Replies: 1
    Last Post: 03-09-2009, 09:10 AM
  5. Create user defined functions in Excel 2007
    By AbhayD in forum Windows Software
    Replies: 3
    Last Post: 24-06-2009, 07:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,967,160.94850 seconds with 17 queries