VBSCRIPT FUNCTIONS

What is a Function?
  •   A function is a group of reusable code which can be called anywhere in your program.
  •  eliminates the need of writing same code over and over again.
  •  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 l. 
  •  There two types of procedures used for VBScript:
     1. Sub procedures - a set of instructions that doesn't return any value.

      2.  Function procedures -a set of instructions that returns a value as output.

A Function procedure:

  • is a series of statements, enclosed by the Function and End Function statements
  • can perform actions and can return a value
  • can take arguments that are passed to it by a calling procedure
  • without arguments, must include an empty set of parentheses ()
  • returns a value by assigning a value to its name

Function Definition :

<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 :
<html>
<body>
<script language="vbscript" type="text/vbscript">
  Function tellHello()
   msgbox("Hello ")
 End Function
 </script>
</body>
</html>

Calling a function

To execute a function, just call it by writing its name (case sensitive) followed by an open parenthesis (and possibly arguments) and then a closing parenthesis: 
Syntax:

Functionname()

OR

Call  Functionname()

Example:
<html>
<body>
<script language="vbscript" type="text/vbscript">
 
 Function tellHello()
   msgbox("Hello ")
 End Function
  Call tellHello()
  </script>
</body>
</html>

Returning a Value from a Function :

  •  A Vb Script function can have an optional return statement. This is required if you want to return a value from a function.
  •  The Function procedure can return a value of the Variant

No comments:

Post a Comment