Functions
Functions
Collage of Engineering
Department of Petroleum
Computer Programming
Lecture Six
Functions
First Year
By
Visual Basic offers a rich assortment of built-in functions. The numeric and string
variables are the most common used variables in programming. Therefore, Visual
Basic provides the user with many functions to be used with a variable to perform
certain operations or type conversion. Detailed description of the function in
general will be discussed in the following functions section. The most common
functions for (numeric or string) variable X are stated in the following table.
Function Description
Y= Math.Abs(X) Absolute of X, |X|
Y= Math.Sqrt(X) Square root of X , √𝑋
Y= Math.Sign (X) (-1 or 0 or 1) for (X<0 or X=0 or X>0)
Y= Math.Exp (X) ex
Y= Math.Log (X) Natural logarithms, ln𝑋
Y= Math.Log(X)/ Math.Log (10) Log𝑋
Y= Math.Sin (𝑋)
Y= Math.Cos (𝑋) Trigonometric functions
Y= Math.Tan (𝑋)
Y= Math.ASin (𝑋) Inverse Trigonometric functions
Y= Math.ACos (𝑋)
Y= tan−1(𝑋) (Where X angle in radian).
Y= Math.ATan (𝑋)
Y= Int(X) Integer of X
Y= Fix(X) Take the integer part
1
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Ex 6-1:
*Private Sub Button1_Click()
Dim num1, num2 As Integer
num1 = Val(TextBox1.Text)
num2 = Math.Abs(num1)
Label1.Text = num2
End Sub
*Private Sub Button2_Click()
Dim num1, num2 As Single
num1 = Val(TextBox2.Text)
num2 = Math.Exp(num1)
Label2.Text = num2
End Sub
*Private Sub Button3_Click()
Dim num1, num2 As Single
num1 = Val(TextBox3.Text)
num2 = Fix(num1)
Label3.Text = num2
End Sub
*Private Sub Button4_Click()
Dim num1, num2 As Single
num1 = Val(TextBox4.Text)
num2 = Math.Log(num1)
Label4.Text = num2
End Sub
*Private Sub Button5_Click()
Dim num1, num2 As Single
num1 = Val(TextBox5.Text)
num2 = Int(num1)
Label5.Text = num2
End Sub
2
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Examples:
Private Sub Button1_Click()
Dim A, B, C, D, M, L, H As String
H= My Name Is
A = LCase(H)
B = UCase(H)
C = Mid(H, 3, 5)
D = LSet(H, 7)
M = RSet(H, 4)
L = Len(A)
TextBox1.Text = A
TextBox2.Text = B
TextBox3.Text = C
TextBox4.Text = D
TextBox5.Text = M
TextBox6.Text = L
End Sub
3
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Ex 6-2: Design a visual basic program with two textboxes. One is used to enter
any string and the other is used to display the inverse of this string.
Private Sub Button1_Click()
Dim x As String
Dim i, y As Integer
x = TextBox1.Text
y = Len(x)
For i = y To 1 Step -1
TextBox2.Text &= Mid (x, i, 1)
Next
End Sub
Visual Basic provides several conversion functions can used to convert values
into a specific data type. The following table describes the convert function.
Function Description
The function CDbl converts, integer, long integer, and single
precision numbers to double-precision numbers. If x is any
CDbl
number, then the value of CDbl(x) is the double-precision number
determined by x.
The function CInt converts long integer, single-precision, and
double precision numbers to integer numbers. If x is any number,
CInt
the value of CInt(x) is the (possibly rounded) integer constant that
x determines.
The function CLng converts integer, single precision and double-
precision numbers to long integer numbers. If x is any number,
CLng
thevalue of CLng(x) is the (possibly rounded) long integer that x
determines.
The function CSng converts integer, long integer, and double-
precision numbers to single-precision numbers. If x is any number,
CSng
the value of CSng(x) is the single-precision number that x
determines.
4
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
The objective of MsgBox is to produce a pop-up message box and prompt the user
to click on a command button before he /she can continues. This format is as
follows:
The first argument, Prompt, displays the message in the message box. The Style
Value determines the type of command buttons appear on the message box, as
shown in Table 10.1. The Title argument will display the title of the message
board.
5
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
yourMsg is a variable that holds values that are returned by the MsgBox ( )
function. The type of buttons being clicked by the users determines the values. It
has to be declared as Integer data type in the procedure or in the general
declaration section. Table 10.2 shows the values, the corresponding named
constant and buttons.
Ex 6-3:
6
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
To make the message box looks more sophisticated, you can add an icon besides
the message. There are four types of icons available in VB2010 as shown in Table
10.3
16 vbCritical
32 vbQuestion
48 vbExclamation
64 vbInformation
7
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Ex 6-4
An InputBox function allows the user to enter a value or a message in a text box.
8
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Ex 6-5:
Private Sub Button1_Click()
Dim userMsg As String
userMsg = InputBox("What is your message?", "Message Entry Form", "Enter
your messge here", 500, 700)
If userMsg <> "" Then
MessageBox.Show("userMsg")
Else
MessageBox.Show("No Message")
End If
9
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Functions and Subroutines are programs designed for specific task, and could
be called from the main program or from sub-procedures without pre definition
or declaration. Users are allowed to call in any number of times which save the
main program space, since it avoids reputation of code these subroutines could
be designed by user or could be previously built.
function procedures and sub procedures share the same characteristics, with
one important difference- function procedures return a value (i. g., give a value
back) to the caller, whereas sub procedures do not.
Function Procedures
10
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Call statement
X= Name (Value1)
Sub Procedures
Call statement
11
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Ex 6-6: Create a program that calculates the cube root of a number, by using
function.
This BMI calculator is a Visual Basic 2010 program that can calculate the body
mass index of a person based on his or her body weight in kilogram and the body
height in meter. BMI can be calculated using the formula weight/ (height )2,
where weight is measured in kg and height in meter.
12
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
If the BMI is more than 30, a person is considered obese. You can refer to the
following range of BMI values for your weight status.
Underweight = <18.5
Normal weight = 18.5-24.9
Overweight = 25-29.9
Obesity = BMI of 30 or greater
Solution
Public Class Form1
Public Function BMI (ByVal height, ByVal weight)
Return (weight) / (height ^ 2)
End Function
13
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
The function to calculate the future value involves three parameters namely the
present value (PV), the interest rate (i) and the length of period (n).
Solution
14
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed
Ex 6-9: Write a code program to read three integer numbers. Using a define sub
procedure (Minimum) to determine the smallest of three integers. Display the
smallest value in TextBox.
Solution
15