Using Functions and Procedures

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Many shorter scripts (those with fewer than 100 lines) are written without using any functions or procedures. Instead, the actions carried out by the script are performed in linear fashion in the main body of the script. This is the recommended way to write short scripts; needlessly wrapping code in a function that is called only once merely makes the script longer, more difficult to read, and harder to maintain.

When working with longer scripts, however, you should place your code in separate functions and procedures whenever possible, and for the following reasons:

  • Functions and procedures easily identify the tasks performed by the script. Without functions and procedures, you must read each line of code just to determine the actions carried out by the script. By using procedures and functions, it is easier to skim through the code and pick out the major tasks.
  • Functions and procedures prevent needless duplication of code. If you find yourself repeating the same lines of code in a script, encapsulate them in a function or procedure. Needless duplication of code creates extra work and can lead to problems if you revise the code and miss one of the instances where the code is used in the script.
  • Functions and procedures make your code portable and reusable. After you write code that successfully performs a task, it is likely to be copied and used in other scripts in your organization. Placing your code in a function or procedure makes the relevant statements easier to identify and copy.
  • Functions and procedures allow your code to be encapsulated in code libraries. Include files are blocks of code that are imported into a script at run time. These files can be referenced in WSH scripts through the use of Windows Script Files.

    Include files enable you to create scripting libraries, collections of code snippets that carry out particular tasks. For example, you might have code that parses command-line arguments. The next time you write a script that requires command-line arguments, you can write a single statement that calls the appropriate script in the code library. Writing the initial code as a function or procedure makes it easier to remove the code from the script itself, place it in a code library, and make it readily available to other scripts and script writers.

  • Functions and procedures make your code easier to test, troubleshoot, and debug. Functions and procedures help you control the lines of code that are actually run during testing. For example, if you suspect your script is encountering problems with a certain block of code, you can comment out calls to other functions and procedures and then test only that block of code.

Calling Functions and Procedures

VBScript provides multiple ways to call functions and procedures. Some require parentheses around parameters; others do not. This is important, because improperly called functions and procedures can either result in errors within the script or, perhaps worse, cause subtle changes that affect the data returned by the script.

Valid ways of calling functions and procedures and passing parameters are shown in Table 18.5. For more information about passing parameters by value and by reference, see "VBScript Primer" in this book.

Table 18.5   Calling Functions and Procedures by Passing Parameters

Passing MechanismSyntax
By reference
  • MyProcedure strVariable
  • MyProcedure strVariable, intVariable
  • Call MyProcedure (strVariable)
  • Call MyProcedure (strVariable, intVariable)
  • Return = MyProcedure(strVariable)
  • Return = MyProcedure (strVariable, intVariable)
By value
  • MyProcedure (strVariable)
  • MyProcedure ((strVariable))
  • Call MyProcedure ((strVariable))
  • Return = MyProcedure((strVariable))
First parameter by reference, second parameter by value
  • MyProcedure (strVariable), intVariable
  • Call MyProcedure((strVariable), intVariable)
  • Return = MyProcedure((strVariable), intVariable)

Note

  • Preparing for Visual Basic .NET. As shown in Table 18.5, the Call statement can be used with VBScript. Because Call is optional in VBScript, and because it cannot be used in Visual Basic .NET, it is recommended that you not use this statement when calling functions and procedures.

Calling functions and procedures in multiple ways can lead to considerable confusion and errors within the script. For example, an error occurs if you use a syntax that inadvertently passes a parameter by value when you intend to pass it by reference (or vice versa). To prevent problems with function and procedure calls, explicitly indicate the passing mechanism when writing the function or procedure. For example, the function shown in the following script snippet makes it clear that the parameter is being passed by value:

Function ConvertToFahrenheit(ByVal sngDegreesCelsius)     ConvertToFahrenheit = (sngDegreesCelsius * (9/5)) + 32 End Function 

As a general rule, pass parameters by value rather than by reference. Passing a parameter by value ensures that your function will not inadvertently change the value of that parameter. This is especially important if the parameter is required elsewhere in the script.

Note

  • Preparing for Visual Basic .NET. If you do not specify ByVal or ByRef when passing a parameter in Visual Basic .NET, the parameter is automatically passed by reference. This is different from previous versions of Visual Basic, in which the default mechanism for passing parameters is by value. To help make your intention clear, explicitly indicate the passing mechanism for all functions and procedures.

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net