Download presentation
Presentation is loading. Please wait.
1
VBScript Session 7 Dani Vainstein
2
What we learn last session?
Interacting in VBScript VBScript constants groups. Dani Vainstein
3
Subjects for session 7 VBScript procedures.
Sub procedures. Function Procedures. Getting data into and out of procedures Call statement. Arguments ByVal and ByRef Dani Vainstein
4
VBScript Procedures In VBScript, there are two kinds of procedures;
the Sub procedure and the Function procedure. Dani Vainstein
5
VBScript Procedures Sub Procedures
A Sub procedure is a series of VBScript statements (enclosed by Sub and End Sub statements) that perform actions but don't return a value. A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses (). Dani Vainstein
6
VBScript Procedures Function Procedures
A Function procedure is a series of VBScript statements enclosed by the Function and End Function statements. A Function procedure is similar to a Sub procedure, but can also return a value. If a Function procedure has no arguments, the Function statement must include an empty set of parentheses. A Function returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a Function is always a Variant. The MsgBox and InputBox, are VBScript functions. Dani Vainstein
7
VBScript Procedures Sub and Function Procedures
If not explicitly specified using either Public or Private, Sub procedures are public by default, that is, they are visible to all other procedures in your script. The value of local variables in a Sub or a Function procedure is not preserved between calls to the procedure. You can't define a Sub procedure inside any other procedure. The Exit Sub, Exit Function statement causes an immediate exit from a Sub or Function procedure. Any number of Exit Sub or Exit Function statements can appear anywhere in a Sub or Function procedure. Dani Vainstein
8
VBScript Procedures Sub Procedures
Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression You call a Sub procedure using the procedure name followed by the argument list. Caution Sub procedures can be recursive, that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. Dani Vainstein
9
VBScript Procedures Function Procedures
To return a value from a function, assign the value to the function name. If no value is assigned to name, the procedure returns a default value a numeric function returns 0 string function returns a zero-length string (""). A function that returns an object reference returns Nothing if no object reference is assigned to name. Dani Vainstein
10
VBScript Procedures Function Procedures
Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure. Dani Vainstein
11
VBScript Procedures Function Procedures
Caution A procedure can use a variable that is not explicitly declared in the procedure, but a naming conflict can occur if anything you have defined at the script level has the same name. If your procedure refers to an undeclared variable that has the same name as another procedure, constant, or variable, it is assumed that your procedure is referring to that script-level name. To avoid this kind of conflict, use an Option Explicit statement to force explicit declaration of variables. Dani Vainstein
12
Function Procedures Caution VBScript may rearrange arithmetic expressions to increase internal efficiency. Avoid using a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression. Dani Vainstein
13
Function Procedures In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. Sub ConvertTemp() Dim Temp temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function Dani Vainstein
14
Getting Data into and out of Procedures
Each piece of data is passed into your procedures using an argument . Arguments serve as placeholders for the data you want to pass into your procedure. You can name your arguments any valid variable name. When you create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. Any arguments are placed inside these parentheses, separated by commas. To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't. Dani Vainstein
15
Sub Implementation Getting an argument Sending an argument
Sub doLoops (iNum) Dim i For i = 1 To iNum Step ‘ do nothing Next End Sub Sending an argument doLoops 5 Call doLoops(5) Dani Vainstein
16
Sub Implementation Getting several arguments Sending an argument
Sub doLoops (iNum,strString) Dim i For i = 1 To iNum Step ‘ do nothing Next End Sub Sending an argument doLoops 5,”Hello” Call doLoops(5,Hello) Dani Vainstein
17
Sub Implementation Getting an argument Sending an argument
Function ByteMe (sngNum) ByteMe = CByte(sngNum) End Function Sending an argument X = ByteMe(5.5) Dani Vainstein
18
Sub Implementation Getting several arguments Sending an argument
Function ByteMe (sngNum,ByRef errNumber) ByteMe = CByte(sngNum) errNumber = Err.Number End Function Sending an argument X = ByteMe(5.5,errNumber) Dani Vainstein
19
Call Statement To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses. The Call statement Transfers control to a Sub or Function procedure. If you omit the Call keyword, you also must omit the parentheses around argumentlist. Dani Vainstein
20
Call Statement If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded. Call MyFunction("Hello World") Function MyFunction(text) MsgBox text End Function Dani Vainstein
21
Arguments ByVal and ByRef
ByVal | ByRef] varname ByVal Indicates that the argument is passed by value. ByRef Indicates that the argument is passed by reference. Dani Vainstein
22
Excercise Dani Vainstein
23
Lab 7.1 Use the InputBox for retrieve two numbers,representing the flanks of a rectangle. Write 2 functions : First function calculates the area of the rectangle Second function calculates the perimeter of the rectangle The results must be displayed in a two lined msg box, each line for each result + information icon + EX 7.1 title. Dani Vainstein
24
Make sure to visit us Tutorials Articles Projects And much more
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.