0% found this document useful (0 votes)
2 views

Functions

The document explains the concept of functions in Visual Basic .NET, highlighting the difference between functions and sub procedures, and detailing user-defined functions. It includes examples such as a BMI calculator and a future value calculator, demonstrating how to create and use functions with arguments. Additionally, it covers how to pass arguments by value and by reference, providing examples for each method.

Uploaded by

mariondominika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Functions

The document explains the concept of functions in Visual Basic .NET, highlighting the difference between functions and sub procedures, and detailing user-defined functions. It includes examples such as a BMI calculator and a future value calculator, demonstrating how to create and use functions with arguments. Additionally, it covers how to pass arguments by value and by reference, providing examples for each method.

Uploaded by

mariondominika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

FUNCTIONS

UNIT: DEVELOP AN INFORMATION SYSTEM

COMPUTER SCIENCE LEVEL 6

PREPARED BY: MR PATRICK


A function is similar to a sub procedure, however,
there is one major difference. A function
returns a value whilst a sub procedure does
not. In Visual Basic .net, there are two types of
functions, the built-in functions and the
functions created by the programmers (user
defined).

17/05/2025 02:19 PM 2
Developing Character, Skills and Competenc
e
Creating User-Defined Functions

Public Function functionName (Argument As dataType,


……….) As dataType
or
Private Function functionName (Argument As dataType,
……….) As dataType
The keyword Public indicates that the function is applicable to
the whole project and the keyword Private indicates that the
function is only applicable to a certain module or procedure. An
argument is a parameter that can pass a value back to the
function.
There is no limit to the number of arguments you can put in.

17/05/2025 02:19 PM 3
Developing Character, Skills and Competenc
e
BMI Calculator

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.
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

17/05/2025 02:19 PM 4
Developing Character, Skills and Competenc
e
EXAMPLE 1
Private Function BMI(Height As Single, weight As
Single) As Double
BMI = weight / Height ^ 2
End Function

Private Sub BtnCal_Click(sender As Object, e As


EventArgs) Handles BtnCal.Click
Dim h As Single, w As Single
h = Val(TextBox1.Text)
w = Val(TextBox2.Text)
LblBMI.Text = BMI(h, w)
End Sub

17/05/2025 02:19 PM 5
Developing Character, Skills and Competenc
e
EXAMPLE 2
FUTURE VALUE CALCULATOR

FV = PV * (1 + i / 100)n
Where PV represents the present value, FV represents the future
value, i is the interest rate and n is the number of periods
(Normally months or years).

17/05/2025 02:19 PM 6
Developing Character, Skills and Competenc
e
Private Function FV(pv As Single, i As Single, n As
Integer) As Double
FV = pv * (1 + i / 100) ^ n
End Function

Private Sub BtnCal_Click(sender As Object, e As


EventArgs) Handles BtnCal.Click
Dim FutureVal As Single
Dim PresentVal As Single
Dim interest As Single
Dim period As Integer
PresentVal = TxtPV.Text
interest = TxtInt.Text
period = TxtN.Text
FutureVal = FV(PresentVal, interest, period)
LblFV.Text = Format(FutureVal, "$#,##0.00")
End Sub
17/05/2025 02:19 PM 7
Developing Character, Skills and Competenc
e
Passing Arguments by Value and by Reference
Functions can be called by value or called by reference. By default,
the arguments in the function are passed by reference. If arguments
are passed by reference, original data will be modified and no longer
preserved. On the one hand, if arguments are passed by value,
original data will be preserved. The keyword to pass arguments by
reference is ByRef and the keyword to pass arguments by value
is ByVal.

For example,

Private Function FV(ByVal pv As Single, ByRef i As Single,


n As Integer) As Double

The function FV receives pv by value, i by reference and n by


reference. Notice that although ByRef is not used to pass n, by default
it is passed by reference.

In this example, we created two functions that compute the square


root of a number , the first uses the keyword ByRef and the second
uses the keyword ByVal.
17/05/2025 02:19 PM 8
Developing Character, Skills and Competenc
e
Private Function sqroot(ByRef x As Single) As Double
x = x ^ 0.5
sqroot = x
End Function
Private Function sqroot1(ByVal y As Single) As Double
y = y ^ 0.5
sqroot1 = y
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Dim u As Single
u = 9
MsgBox(3 * sqroot(u), , "ByRef")
MsgBox("Value of u is " & u, , "ByRef")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
Dim u As Single
u = 9
MsgBox(3 * sqroot1(u), , "ByVal")
MsgBox("Value of u is " & u, , "ByVal")
17/05/2025 02:19 PM 9
End Sub Developing Character, Skills and Competenc
e

You might also like