VBScript - Introduction to functions and procedures

By Bryan Muehlberger, ITworld |  How-to

Last week we discussed the basics of commenting your code. Today, we
move to a more advanced capability within VBScript and introduce the
concepts of functions and procedures.

The main purpose of functions and procedures is to create segments of
code that are reusable, whether within your script, or within other
scripts. For example, you may need to find the result of adding four
numbers together and dividing them by four (i.e. the average). If you
have to do this more than once within your script, it may make sense to
create a function or procedure. In this case, a function may be better,
but this will depend on your needs.

Before we go any further, it is important to understand the similarities
and differences between functions and procedures. Functions are defined
using the 'Function' keyword, whereas procedures are defined using the
'Sub' keyword. Both functions and procedures can take arguments, process
a list of statements, and modify the values of its arguments. In
addition, when calling functions the arguments are always put in
parenthesis, whereas when calling a procedure, if you want to use
parenthesis around your argument list you must preface the procedure
name with the 'Call' keyword. Last, functions have the ability to
"return" values to the calling statement, whereas procedures cannot.

This last difference is important to understand, so before we go into
the syntax of creating and using functions and procedures, we will go
into an example to explain this difference.

Let's say that you need to find the average of four numbers (as given
above), you might have a routine called "average4nums". If this routine
was defined as a function (using the 'Function' keyword), you could do
the following:

Result = average4nums(5, 11, 3, 9) 'Call your Function and return the
result to 'Result'

Notice that I was able to use my routine (i.e. my function), on the
right hand side of my statement. This is because a function can return
a value. In this case when average4nums is called with the arguments
(5, 11, 3, 9), it will compute the result and return seven to the
calling statement. After the value is returned, the variable 'Result'
will be assigned to the value computed within my function, which in this
case is seven.

However, if you had used a procedure (defined using the 'Sub' keyword),
you would have been able to compute the same result, but it would have
had to be called as follows:

Call average4nums(5, 11, 3, 9) 'Call your Procedure

Then to be able to obtain the result of the routine, you would have had
to assign a variable within the procedure with the result.

Join us:






Spotlight on ...
Online Training

    Upgrade your skills and earn higher pay

    Readers to share their best tips for maximizing training dollars and getting the most out self-directed learning. Here’s what they said.


    Learn more

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question