VBScript Procedures

Advertisements


What is a Function?

A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing same code over and over again. This will enable programmers to divide a big program into a number of small and manageable functions.

Apart from inbuilt Functions, VBScript allows us to write user-defined functions as well. This section will explain you how to write your own functions in VBScript.

Function Definition :

Before we use a function, we need to define that particular function. The most common way to define a function in VBScript is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with a End Function keyword, which indicates the end of the function. The basic syntax is shown below:

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

Function Functionname(parameter-list)
  statement 1
  statement 2
  statement 3
  .......
  statement n
End Function

</script>
</body>
</html>

Example :

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

 Function sayHello()
   msgbox("Hello there")
 End Function

</script>
</body>
</html>

Calling a Function :

To invoke a function somewhere later in the script, you would simple need to write the name of that function with the Call keyword.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

 Function sayHello()
   msgbox("Hello there")
 End Function

 Call sayHello()
  
</script>
</body>
</html>

Function Parameters:

Till now, we have seen function without a parameter, but there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. The Functions are called using the Call Keyword.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

Function sayHello(name, age)
   msgbox( name & " is " & age & " years old.")
End Function

Call sayHello("Tutorials point", 7)

</script>
</body>
</html>

Returning a Value from a Function :

A VBScript function can have an optional return statement. This is required if you want to return a value from a function.

For example, you can pass two numbers in a function and then you can expect from the function to return their multiplication in your calling program.

NOTE : A function can return multiple values separated by comma as an array assigned to the function name itself.

Example :

This function takes two parameters and concatenates them and returns result in the calling program. In VBScript, the values are returned from a function using function name. In case if you want to return two or more values, then the function name is returned with an array of values. In the calling program, the result is stored in the result variable.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

  Function concatenate(first, last)
    Dim full
    full = first & last
    concatenate = full  'Returning the result to the function name itself
  End Function

</script>
</body>
</html>

Now, we can call this function as follows:

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
 Function concatenate(first, last)
    Dim full
    full = first & last
    concatenate = full  'Returning the result to the function name itself
  End Function
  ' Here is the usage of returning value from function. 
  dim result
  result = concatenate("Zara", "Ali")
  msgbox(result)
</script>
</body>
</html>

Sub Procedures :

Sub Procedures are similar to functions but there are few differences.

  • Sub procedures DONOT Return a value while functions may or may not return a value.

  • Sub procedures Can be called without call keyword.

  • Sub procedures are always enclosed within Sub and End Sub statements.

Example :

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

 Sub sayHello()
   msgbox("Hello there")
 End Sub

</script>
</body>
</html>

Calling Procedures :

To invoke a Procedure somewhere later in the script, you would simply need to write the name of that procedure with or without the Call keyword.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

 Sub sayHello()
   msgbox("Hello there")
 End Sub
 sayHello()

</script>
</body>
</html>

Advanced Concepts for Functions

There is lot to learn about VBScript functions. We can pass the parameter byvalue or byreference. Please click on each one of them to know more.



Advertisements
Advertisements