Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Wednesday, 29 January 2014

Procedures And Functions



Procedures And Functions

There are two kinds of procedures in VBScript: A sub procedure and a function. The difference lies on their behaviors but their coding (programming) depends of your goal

A procedure can be included in the body of an HTML but to separate the script behavior from the rest of the file, it is usually a good idea to include the procedures in the head section of the file.

The advantage of including a script in the head section is that it is more likely to be interpreted before the section it refers to is reached. If you have done any type of programming before, you may know that interpreters (and compilers) read a program in a top-down approach. Therefore, if the browser (actually the VBScript interpreter) finds a thing in the body section but doesn't know what that thing is because it is in the bottom part of the body section, it may not interpret your script accurately. But if the script is in the head section, the interpreter will have "seen" it before reaching the body section

Sub Procedures

A sub procedure is a section of code that carries an assignment but doesn't give back a result. To create a sub procedure, start the section of code with the Sub keyword followed by a name for the sub procedure. To differentiate the name of the sub procedure with any other regular name, it must be followed by an opening and closing parentheses. The section of the sub procedure code closes with End Sub as follows:

Sub ShowMeTheDough()

End Sub

We can declare variables in the procedure if you need to. These variables are declared and dealt with way, in the same way we learned in the regular script sections. Using declared variables, the above procedure can be written as follows:

Sub DisplayFullName()
Dim FirstName, LastName
Dim FullName

FullName = FirstName & " " & LastName
End Sub

Calling a Procedure

After creating a procedure, you can call it from another procedure, function, or control's event in the body section of an HTML file. To call a simple procedure such as the earlier DisplayFullName, you can just write the name of the sub procedure

In the following example, the above DisplayFullName sub procedure is called when the user clicks the Detail section of the form:

Sub Detailer()
DisplayFullName
End Sub

If you want the procedure to be accessed immediately as soon as the page displays, you can assign its name to the onLoad() event of the body tag

Arguments Passing

To carry an assignment, sometimes a procedure needs one or more values to work on. If a procedure needs a variable, such a variable is called an argument. Another procedure might need more that one argument, thus many arguments. The number and types of arguments of a procedure depends on various factors.

If you are writing your own procedure, then you will decide how many arguments your procedure would need. You also decide on the type of the argument(s). For a procedure that is taking one argument, in the parentheses of the procedure, write a name for the argument. Here is an example:

Sub CalculateArea(Radius)
Dim dblPI
Dim dblArea

dblPI = 3.14159
dblArea = Radius * Radius * dblPI
End Sub

A procedure can take more than one argument. If you are creating such a procedure, between the parentheses of the procedure, write the name of the first argument followed by a comma; add the second argument and subsequent arguments and close the parentheses. There is no relationship between the arguments; for example, they can be of the same type:

Sub CalculatePerimeter(Length, Height)
Dim dblPerimeter

dblPerimeter = 2 * (Length + Height)
End Sub
The arguments of your procedure can also be as varied as you need them to be. Here is an example:
Sub DisplayGreetings(strFullName, intAge)
Dim Sentence
Sentence 

Calling an Argumentative Procedure

We saw already how to call a procedure that doesn't take any argument. Actually, there are various ways you can call a sub procedure. As we saw already, if a sub procedure doesn't take an argument, to call it, you can just write its name. If a sub procedure is taking an argument, to call it, type the name of the sub procedure followed by the name of the argument. If the sub procedure is taking more than one argument, to call it, type the name of the procedure followed by the name of the argument, in the exact order they are passed to the sub procedure, separated by a comma. Here is an example:

Sub Result()
Dim dblHours, dblSalary

CalcAndShowSalary dblHours, dblSalary
End Sub
Sub CalcAndShowSalary(Hours, Salary)
Dim dblResult

dblResult = Hours * Salary
txtResult = dblResult
End Sub

Alternatively, you can use the keyword Call to call a sub procedure. In this case, when calling a procedure using Call, you must include the argument(s) between the parentheses. using Call, the above procedure could call the CalcAndShowSalary as follows:

Sub Result()
Dim dblHours As Double
Dim dblSalary As Double

dblHours = txtHours
dblSalary = txtSalary

Call CalcAndShowSalary(dblHours, dblSalary)
End Sub

Functions

Creating a Function

A function is an assignment that a piece of code can take care for the functionality of a database. The main difference between a sub procedure and a function procedure is that a function can return a value

A function is created like a sub procedure with a few more rules. The creation of function starts with the Function keyword and closes with End Function. Here is an example:

Function FindFullName()

End Function

The name of the function follows the same rules and suggestions we have reviewed for the sub procedures.

To implement a function, remember that it is supposed to return a value. In the body of the function, describe what it is supposed to do. to return the right value, assign the desired value to the name of the function. Here is an example:

Function CalculateArea(Radius)
CalculateArea = Radius * Radius * 3.14159
End Function

A function can also be as complex as performing many and various expressions in order to get a value that can be assigned to the name of the function.

Calling a Function

To call a function, you have two main alternatives. If you want to use the return value of a function in an event or another function, assign the name of the function to the appropriate local variable. Make sure you include the argument(s) of the function between parentheses.

No comments:

Post a Comment