Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
VBS Basics
Introduction
Variables & Arrays
Sub & Function Procedure
If Statements
Case Statements
Loop Statements
Operators
Forms
MsgBox Function
Books @
CA Amazon
UK Amazon
US Amazon
 

VB Script 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. A Function procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure). If a Function procedure has no arguments, its 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.[msdn.microsoft]
Here is a syntax for none-parameterized function procedure:
function userName()
 userName="Clay"
end function
. This function remembers user name. You can use this function any number of times in your program and you change it once in here to maintain. Use userName() to call this function. eg; document.write("The usre name is: "& userName()).
Here is a syntax for parameterized function:
Function total(price)
  dim tax
  tax=price*.09
  total=price+tax
end Function
. The price value is provided when calling the function. eg; document.write("Total price is: " & total(50)).

The following example is function procedure that takes users first name and last name. The values are genetically provided by the user using input box. The function returns users full name on message box.
<html>
<head>
<script language="vbscript">
function userName(first,last)
  userName=first&" "&last
end function
</script>
</head>
<body>
<script language="vbscript">
  dim firstName
  dim lastName
  firstName=inputbox("Enter your first name")
  lastName=inputbox("Enter your last name")
  msgbox("Your full name is: "& userName(firstName,lastName))
</script>
</body>
</html>
This example has two scripts. The function in the head section of the document and the statements in the body of the document. The function simply takes two parameters, first & last and concatenates into function return value userName. The second part declares two variables, firstName and lastName then prompts input boxes. The typed value are stored in these two variables. The two values are passed into the function which prompts back the concatenation of the two values.

VB Script Predefined Functions

These are built-in VBScript functions. All you have to do is pass proper values to these function and they will respond as they built for.
String Functions Function Decriptions Function Example
Asc Returns the ASCII character code for the first character of a string.document.write(ASC ("ABC")). returns: 65
Chr Returns the corresponding character of passed ASCII code number.document.write((Chr(65))). returns: A
LCase Converts all the characters to lowercase.document.write(lcase("CASE")). returns: case
UCase Converts all the characters to upper case.document.write(Ucase("Case")). returns: CASE
Left Returns specified number of characters starting from left.document.write(left("language",3)). returns: lan
Right Returns specified number of characters staring from right. document.write(Right("language")). returns: age
LTrim Removes all the leading spaces from the string.document.write(LTrim("far ")). returns: far, with no spaces
RTrim Removes all the trailing spaces rom the string.document.write(RTrim(" far")). returns: far, with no spaces
TrimReturns the string argument, with leading and trailing spaced removed. document.write(Trim(" mid ")). returns: mid, with no spaces.
ValReturns number value of a string. Stops as soon it hits string char. document.write(Val("22 Maple st.")). returns: 22
StringTakes a number and character code argument and returns the character, repeated a number of times: document.write(String(3,80)). returns: BBB
Date Functions
Now()Returns current system date and time.document.write(now()). returns: 8/2/02 10:46:33 AM ->with current date & time
DateReturns the current system date in month, day, year format. document.write(date()). returns: 8/2/02 ->with current date
TimeReturns the current system time. document.write(Time()). returns: 10:11:36 AM ->with current time.
DayReturns the day of the month. document.write(Day(date)). returns: 9 ->with current day number.
MonthReturns the month of the year. document.write(Month(date)). returns: 9 ->with current month number.
MonthNameReturns the name of the specified month. document.write(MonthName(9)). returns: September
WeekDayReturns number representing the day of the week.document.write(WeekDay(date)). returns: 6 -> Assuming today is Friday
WeekdayNameReturns the name of the specified week day.document.write(WeekdayName(6)). returns: Friday
DateAddAdds the specified date or time value to specified date.document.write(DateAdd("d","10","6-7-02")). returns: 6/17/02 -> Adds 10 days to specified date. d=day, m=month, y=year, h=hour, n=minute
Hour, Minute, SecondReturns the number representing hour, minute, second of the day, hour, or minute.document.write(Hour()). returns: 11 -> Assuming it's the hour is 11.
TimerReturns the number of seconds since 12:00 AM.document.write(Timer). returns: 40055.84 -> at 11:07 AM
TimeSerialReturns the time for a specific hour, minute, and second converted from military clock.document.write(TimeSerial(17,30,05)). returns: 5:30:05 PM.
TimeValue(time)Returns the current system time.document.write(Time(time))returns: 11:18:00 AM -> with current time.
FormatDateTimeReturns a formatted date or time.document.write(FormatDateTime(date)). returns: 8/2/02-> with current date.
Number Functions
AbsReturns the absolute value of a number.document.write(Abs(-5)). returns: 5
IntReturns the integer part of a number.document.write(Int(5.567)). returns: 5
FixReturns the integer part of a number.document.write(fix(5.567)). returns: 5
SqrReturns square root of a number.document.write(sqr(25)). returns: 5
SgnReturns 1, 0, or -1 for possitive, zerro or negative numberdocument.write(Sgn(-22)). returns: -1
SinReturns sine value of a number.document.write(sin(1)). returns: 0.841470984807897
TanReturns tangent value of a number.document.write(tan(1)). returns: 1.5574077246549
CosReturns cosine value of a number.document.write(Cos(1)). returns: 0.54030230586814
RndReturns random number.document.write(Rnd). returns: 0.7055475 ->Needs some code to randamize.
HexReturns the hexadecimal value of a number.document.write(Hex(10)). returns: A
RoundReturns rounded number.document.write(Round(10.888)). returns: 11
IsNumericReturns boolean value [true or false] indicates whether is number or not.document.write(IsNumeric(10.888)). returns: True
FormatNumberReturns an expression formatted as a number value.document.write(FormatNumber(1000.1111,2)). returns: 1,000.11
FormatPercentReturns an expression formatted as a percentdocument.write(FormatPercent(.22)). returns: 22.00%
FormatCurrencyReturns an expression formatted as a currency value.document.write(FormatCurrency(10.888)). returns: $10.89
Other Functions
MsgBoxReturns dialog box with specified expression or arguments.msgbox"Hello World" returns: Hello World -> On dialog box.

Sub Procedures If statement
Bluehost.com Web Hosting $6.95
PC System Tools
SpyCleaner
Registry Mechanic
4Diskclean Pro
Health PC Club
TweakNow
Registry Washer
SpyAnyWhere
System Lifeguard
...More/Details

PC Articles
  Computer Safety
prevent viruses & Spyware