Function methods (we will call them "functions" for short) are similar in that they do some computation, but they communicate the result to the rest of the program by returning a value.
This note introduces some of the function methods of the Math class.
The following is a listing of a Math function program that uses five functions of the Math class: Sqrt, Round, Floor, Min, and Max:
Private Sub BtnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEvaluate.Click lbl1.Text = CStr(Math.Sqrt(25)) lbl2.Text = CStr(Math.Sqrt(5 + 10 + 10)) lbl3.Text = CStr(Math.Round(7.49)) lbl4.Text = CStr(Math.Round(7.51)) lbl5.Text = CStr(Math.Floor(7.1)) lbl6.Text = CStr(Math.Floor(7.99)) lbl7.Text = CStr(Math.Min(1, 2)) lbl8.Text = CStr(Math.Max(1, 2)) End Sub
When you click the Evaluate the Functions button, each function is evaluated and the value it returns is displayed.
The Sqrt function returns the square root of a specified number. Round rounds a number up or down to the nearest whole number. Floor returns the largest whole number that is less than a number (truncating the decimal portion of a floating point number), and Min and Max return the smallest or largest of two numbers.
These are summarized in the following table:
Function | Returns | Syntax | Data type | Number of Arguments |
---|---|---|---|---|
Sqrt | the square root of a number | Math.Sqrt (<numeric expression>) | double | 1 |
Round | A number rounded off | Math.Round (<numeric expression>) | double | 1 |
Floor | a number with the decimal portion truncated | Math.Floor (<numeric expression>) | double | 1 |
Min | the smallest of two numbers | Math.Min (<numeric expression>, <numeric expression> ) | same as arguments | 2 |
Max | the largest of two numbers | Math.Max (<numeric expression>, <numeric expression> ) | same as arguments | 2 |
Like an expression, a function returns a single value when evaluated. Functions also have data types determined by the type of the value they return.
The basic syntax of all functions is the same: the class and name of the function followed by parenthesized arguments. The arguments are used in the computation. For example, the Sqrt function takes one argument, a numeric expression, and returns it's square root.
If a function requires more than one argument, they are separated by commas. For example, the Min function requires two arguments, both numeric expressions. The numeric expressions must be of the same type, which will determine the type of the returned value.
Note that some function do not require any arguments, and they are called with empty parenthesis ().
These are just a few of the functions of the Math class (most of them have to do with trigonometry) and VB comes with many other classes with function methods.
Like the Show method of the MessageBox class, these Math methods are shared. They are associated with the class itself, not a particular instance of the class. Their syntax is
<class>.<method>
not
<object>.<method>
Functions are a way to capture a certain computation for convenient re-use. But, we are not limited to the functions that come with VB. In a subsequent note, you will learn to write your own functions.