Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

Built In Functions

Built In Functions accept one or more


arguments, and return a value based on the
arguments. VB provides many built-in
functions such as

--- String Functions


--- Mathematical Functions
--- Date and time Functions
--- Format Functions
--- Data Conversion Functions
String Manipulation Function
Some of the string manipulation function are Len, Right, Left, Mid, Trim, Ltrim, Rtrim,
Ucase, Lcase, Instr, Val, Str ,Chr and Asc.
1) The Len Function
The length function returns an integer value which is the length of a phrase or
a sentence, including the empty spaces.
Syntax : Len (“Phrase”)
Example: Len (VisualBasic) = 11
Len (welcome to VB tutorial) = 22
The Len function can also return the number of digits or memory locations of a
number that is stored in the computer.
Example
Dim y, lengthy as integer
Y=1234
Lengthy=Len(y)
Print lengthy
End sub Output : 4
2) The Right Function
The Right function extracts the right portion of a phrase.
Syntax: Right (“Phrase”, n)
Where n is the starting position from the right of the
phrase where the portion of the phrase is going to be
extracted.
Example: Right(“Visual Basic”, 4) = asic
3) The Left Function
The Left function extract the left portion of a phrase.
Syntax : Left(“Phrase”, n)
Where n is the starting position from the left of the phase
where the portion of the phrase is going to be
extracted.
Example: Left (“Visual Basic”, 4) = Visu
4) The Ltrim Function
The Ltrim function trims the empty spaces of the left portion of
the phrase.
Syntax: Ltrim(“Phrase”)
Example: Ltrim (“ Visual Basic”, 4)= Visual basic
5) The Rtrim Function
The Rtrim function trims the empty spaces of the right portion
of the phrase.
Syntax : Rtrim(“Phrase”)
Example: Rtrim (“Visual Basic ”, 4) = Visual basic
6) The Trim function
The Trim function trims the empty spaces on both side of the
phrase.
Syntax: Trim(“Phrase”)
Example : Trim (“ Visual Basic ”) = Visual basic
7) The Mid Function
The Mid function extracts a substring from the original
phrase or string.
Syntax: Mid(phrase, position, n)
Where position is the starting position of the phrase from
which the extraction process will start and n is the number of
characters to be extracted.
Example: Mid(“Visual Basic”, 3, 6) = sual B
8) The InStr function
The InStr function looks for a phrase that is embedded within
the original phrase and returns the starting position of the
embedded phrase.
Syntax : Instr (n, original phase, embedded phrase)
Where n is the position where the Instr function will begin to
look for the embedded phrase.
Example : Instr(1, “Visual Basic”,” Basic”) = 8
9) The Ucase function
The Ucase function converts all the characters of a string to capital
letters.
Example: Ucase(“Visual Basic”) =VISUAL BASIC
10) The Lcase function
The Lcase function converts all the characters of a string to small letters.
Example: Lcase(“VisUAl BaSic”) =visual basic

11) The Str Function


The Str is the function that converts a number to a string.
Example:
Dim curDate as Date
curDate = Now()
MsgBox(CStr(curDate)) --- Produces a message box with the
current date and time.

12) The Val Function


The Val function converts a string to a number.
Example : a=val(text1.text)
13. The Chr Function
The Chr function returns the string that corresponds to
an ASCII code. ASCII stands for “American Standard
Code for Information Interchange”.
Syntax: Chr(charcode)
Example : Chr(65)=A
Chr(122)=z
Chr(37)=%
14. The Asc Function
The Asc function converts an ASCII character or symbol to
the corresponding ASCII code.
Syntax: Asc(Character)
Example : Asc(“B”)=66
Asc(“&”)=38
15) The String function
The String function has two arguments, a number and
a single-character string, and returns a string
consisting of the specified character repeated the
specified number of times.
Syntax: String(n,"Character")
Example: String(30, "#") will return the # symbol 30
times.
Private Sub Form_Load()
Print String(30, "#")
End Sub
Output:
#################################
Mathematical Functions
The common mathematical functions in Visual Basic
are Rnd, Sqr, Int, Abs, Exp, Log, Sin, Cos, Tan
Fix and Round.
1) The Rnd Function
Rnd is is very useful function for dealing with the concept of chance and
probability. The Rnd function returns a random value between 0 and 1.

Example Random Number Generation


When you run the program, you will get an output of 10 random numbers
between 0 and 1.

Private Sub Form_Load


Dim x as integer
For x=1 to 10
Print Rnd
Next
End Sub
2) The Int Function:
The int function is the function that converts a number into an integer
by truncating its decimal part and the resulting integer is the largest
integer that is smaller than the number.
Example: Int(2.4)=2, Int(4.8)=4, Int(-4.6)= -5,
Int(0.032)=0

3) The Sqr Function :


Sqr is the function that computes the square root of a number.
Example: Sqr(4)=2
Sqr(9)=2

Private sub Command1_Click()


Dim myNumb as Double
myNumb = Sqr(47)
Print myNumb Output
End sub myNumb = 6.85565460040104
4) The Abs Function
Abs is the function that returns the absolute value of a
number.
Example: Abs(-8) = 8
Abs(8)= 8.
Dim a,b as Double
a = Abs(3.14159)
b = Abs(-42)
Output
a= 3
b= 42

5) The Exp Function


Exp of a number x is the value of ex.
Example: Exp(1)=e1 = 2.7182818284590
6) The Round Function
Round is the function that rounds up a number to a
certain number of decimal places.
Syntax: Round (n, m) - which means to round a number
n to m decimal places.
Example: Round (7.2567, 2) =7.26
7) The Log function
The Log is the function that returns the natural Logarithm
of a number.
Example : Log (10)= 2.302585
Geometrical Functions : Geomotry functions to calculate
Cosine, Sine, Tangent etc
Cos([angle]))
Sin([angle]))
Tan([angle]))
Date and Time Functions
Date and time functions allows us to perform various
operations related to date and time in Visual Basic.

1)Now Function:
It Returns both date and time stored in computer's
clock.

Syntax: Now()
Example : Print Now()
Output is 07/12/2017 11:14:58 PM
2) Date Function
Returns the system date.
Syntax: Date()
Example :
Print Date() ---- > Output is 07/12/2017

3) Time Function
Returns System time.

Syntax: Time()
Example:

Print time() -- > Output is 11:20:04 PM


4) DatePart Function:
It is used to extract various parts of the date/time.
Syntax : DatePart(interval, validDate)
' interval : String part you want to return.
' validDate : Variant(Date) that you want to evaluate.
'Interval's:
"yyyy" Year
"q" Quarter
"m" Month
"y" Day of year
"d" Day
"w" Weekday
"ww" Week
"h" Hour
"n" Minute
"s" Second
Example :
Print "Current Month is " & DatePart("m", Now)
Output  Current Month is 7
5) Day, Month, year Functions
Returns their Day, month and year in numeric form
Syntax :
Day(DateArgument)
Month(DateArgument)
Year(DateArgument)

Example :
Print Day(Now) Output is 11
Print Month(Now)  Output is 7
Print Year(Now)  Output is 2017
6) Hour, Minute and Second Functions
Returns hour, minute, and seconds in numeric form
Syntax:
Hour(DateArgument)
Minute(DateArgument)
Second(DateArgument)
Example:

Print Hour(Now)  Output is 23


Print Minute(Now)  Output is 32
Print Second(Now)  Output is 56

Current Time is 11:32:56 PM


7) Timer function
Returns number of seconds passed since midnight.

Syntax:
Timer()

Example:
Print Timer()  Output is 84878.33
i.e, this much seconds have passed since midnight.
current time is 11:35 PM with some seconds.
Formatting Functions
The three most common formatting functions in VB
are Tab, Space, and Format
1)The Tab function
Syntax : Tab (n); x
• The item x will be displayed at a position that is n
spaces from the left border of the output form.
• There must be a semicolon in between Tab and the
items you intend to display.
Example
Private Sub Form_Load
Print "I"; Tab(5); "like"; Tab(10); "to"; Tab(15); "learn"; Tab(20);"VB"
Print
Print Tab(10); "I"; Tab(15); "like"; Tab(20); "to"; Tab(25); "learn"; Tab(20); "VB"
Print
Print Tab(15); "I"; Tab(20); "like"; Tab(25); "to"; Tab(30);"learn"; Tab(35); "VB"
End sub
2) The Space function
The Space function is very closely linked to the Tab
function. However, there is a minor difference. While
Tab (n) means the item is placed n spaces from the
left border of the screen, the Space function specifies
the number of spaces between two consecutive
items.
Example
Private Sub Form_Activate()
Print "Visual"; Space(10);"Basic"
End Sub

Output  Visual Basic


3)The Format function
The Format function can display the numeric values in
various forms.
Syntax : Format (n, “style argument”)
where n is a number and the list of style arguments
Example
Private Sub Form_Activate()
Print Format (8972.234, "General Number")
Print Format (8972.2, "Fixed")
Print Format (6648972.265, "Standard")
Print Format (6648972.265, "Currency")
Print Format (0.56324, "Percent")
End Sub
DATA CONVERSION FUNCTIONS
Using these functions we can convert the value to the required data
type.
Asc()  Converts a string argument to the ASCII code .
CCur() Converts the argument to an equivalent Currency data type.
CDbl()Converts the argument to an equivalent Double data type.
CInt()Rounds a floating-point number up to the next whole
number.
CLng()Converts the argument to an equivalent Long data type.
CSng()Converts the argument to an equivalent Single datatype.
CStr()Converts the argument to an equivalent String datatype.
CVar()Converts the argument to an equivalent Variant datatype.
Fix()Truncates the fractional portion of a floating-point number.
Int()Rounds a floating-point number down to the next whole
number (integer).

You might also like