Page 54 - Computing Book 8
P. 54
Developing Applications – Visual Studio Class 8
Functions, Subroutines, and Modules:
A Visual Basic 2010 function is a type of procedure that returns a value that is passed on to the main
procedure to finish execution. A function is similar to a subprocedure but there is one major
difference, a function returns a value whilst a subprocedure does not.
In Visual Basic 2010, there are two types of functions, the built-in functions and the functions
created by the programmers. Functions created by the programmer are also known as user-defined
functions. In this lesson,
Creating User-Defined Functions:
To create a user-defined function in Visual Basic 2010, you can use the following syntaxes:
Public Function functionName (Argument As dataType,……….) As dataType
OR
Private Function functionName (Argument As dataType,……….) As dataType
The keyword Public indicates that the function applies to the whole project and the keyword Private
indicates that the function only applies to a certain module or procedure. The argument is a
parameter that can pass a value back to the function. There is no limit to the number of arguments
that can be added.
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 kilograms and body height in meters. 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+
Follow these steps to create a program to calculate BMI:
• Create a new form with four labels, two text boxes, and a button.
• Change the text of all labels as shown in the example.
th
• Change the name of the 4 label(result) to “lblBMI”.
• Change the text of the button to “Calculate BMI”
• Double click on the button to open the script window.
• Enter the following code to create a function.
Private Function
Variable declaration.
Values from TextBoxes.
After calculation from
function, lblText will
have the value of BMI
The City School /Academics/Computing Curriculum/Class 8/2020-2021 Page 53 of 75

