Chapter 18 Functions

Functions are modules that accept parameters and return a single value which is assigned as a value to a variable in the main program. A function usually performs a task that is required many times within the program.

Visual Basic allows data to be passed into a function by value such that  the value of the original data is not changed or passed into and out of a function by reference such that  the value of the original data is changed.

An example of this is when two measurements of a rectangle are passed in and the function returns the Area or the Perimeter of the rectangle. The following program will allow the calculation of the Area and Perimeter of a rectangle when the length and width are entered.

Rectangle problem

You are to write a Visual Basic program that will allow a user to enter two measurements - one in each of two text boxes for length and width. When the Area button is clicked, the area is calculated and put into the Area label. When the Perimeter button is clicked, the perimeter is calculated and put into the perimeter label.

Planning

A function called Area is to be created that accepts two numbers as parameters by value. The function calculates the Area and returns this to the main program.

A function called Perimeter is to be created that accepts two numbers as parameters by value. The function calculates the Perimeter and returns this to the main program.

Pseudcode

  Function CalcArea(Side1, Side2)
      Set CalcArea  To Side1* Side2
end function
  
  Function CalcPerimeter(Side1, Side2)
      Set CalcPerimeter To 2 * (Side1 + Side2)
end function
  

Mainline:

Read Length
Read Width

Area = CalcArea(length, width)

Display area

Perimeter = CalcPerimeter (length, Width)

Display Perimeter

End

Coding

'the functions should be place just under the heading Public Class Form 1


Public Function ComputeArea(ByVal Length As Integer, ByVal Width As Integer) As Integer
'this function receives length and width and returns area
ComputeArea = Length * Width
End Function

Public Function ComputePerimeter(ByVal Length As Integer, ByVal Width As Integer) As Integer
'this function receives length and width and returns perimeter
ComputePerimeter = 2 * (Length + Width)
End Function

Private Sub cmdArea_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdArea.Click


Dim l, w, a As Integer
l = Val(txtLength.Text)
w = Val(txtWidth.Text)
a = ComputeArea(l, w)
lblArea.Text = a


End Sub

Private Sub cmdPerimeter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPerimeter.Click


Dim len, wid, perim As Integer
len = Val(txtLength.Text)
wid = Val(txtWidth.Text)
perim = ComputePerimeter(len, wid)
lblPerimeter.Text = perim


End Sub

Explanation

When the Area or Perimeter button is clicked, the values in the text boxes are copied into the variable l and b. These are then passed by value into the Area or Perimeter function as the variables length and width. 

The appropriate formula is applied and a value calculated (area or perimeter). This is then assigned to the function and that value is returned to the main program where it is displayed in a label.

Exercise 18

Problem 1

Modify the  functions above to accept one values for the radius of a circle and return area and circumference.  

Problem 2

Write a function that will accept two string values and return a True if they are identical and a False if they are not.

Problem 3 

Write a function that will accept a number value representing a students exam mark between 0 and 100, and return a grade of A, B, C, D or E depending on the exam mark.


Original Author Mike Leishman Last Updated 17 April 2000

edited for VB.Net by FDS 29/4/2007