www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons VBScript Functions Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
VBScript functions are self contained, named blocks of code that perform some function or task.

VBScript functions allow us to reuse our code again and again. For example we can write a function that performs the concatenation of two strings. After writing the function whenever we need the functionality of the concatenation of two strings, we can CALL our string concatenation function.

VBScript functions are defined using the Function and End Function keywords. The code that makes up the function is written between these two keywords.

Here are the steps to write VBScript functions:

  1. Mark the start of the function with the Function keyword.
  2. Give your function a meaningful name.
  3. Follow the function name with a pair of round brackets.
  4. If the function is expecting external information to perform its task, add the name(s) for the external information, also called arguments. This is optional.
  5. Starting on a new line, write the code that makes up the function.
  6. Finish with the End Function keywords.

Here is a simple example that requires two numbers as arguments and sums them both:

Function add(num1, num2)
add = num1 + num2
End Function

Calling VBScript Functions
A function doesn't do anything until it is called. Calling a function in VBScript is very easy. We call a function by writing its name followed by a pair of brackets. Optionaly, if the function requires external information, we have to pass it between the pair of brackets.

Here is a simple Example of calling our add function, above:

add(10, 5)

In the above line of code we call our add function, passing in two values, also called parameters.

Here is an example:

<script type="text/vbscript">
Function add(num1, num2)
add = num1 + num2
End Function

Dim total
total = add(10, 5)
document.write("The total of 10 and 5 is: " & total)
</script>

Here is the output:

The total of 10 and 5 is: 15


Next >>> Lesson No. 14: VBScript Message Box






Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.