VBScript - Introduction to functions and procedures

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. Now we have

not discussed scope before, but this variable would have to be defined

within the main part of your script so that it would be 'accessible'

within both your procedure routine and your main section of code,

similar to the following:

Dim Result

Call average4nums(5, 11, 3, 9) 'Result is assigned value of 7 within

your Procedure

Wscript.echo Result

Now that you understand the differences between functions and

procedures, next week we will go into the syntax of creating and using

functions and procedures within your scripts.

What’s wrong? The new clean desk test
Join the discussion
Be the first to comment on this article. Our Commenting Policies