Function Procedure in VB.NET


A Function procedure is a group of logically related statements that is used to perform a specific task.
You can pass arguments to the Function procedure while calling it.
It can also return a single value to the point from which it is called. Thus the only difference between Sub procedure and Function Procedure is that sub procedure does not return any value while function procedure returns a single value.
Function procedure can be defined using Function and End Function keywords.
The general syntax for defining a Function procedure is given below:

[Scope] Function ProcedureName ([Argument List]) [As Return Type]
Group of Related statements
[Return Statement]
End Function
Here,
(1) Scope: It is optional. It defines scope of the procedure. It can have either Public, Private or Protected scope. If don’t specify then by default it is Public.
(2) Procedure Name is the name of function procedure that you want to define.
(3) Argument List is optional. Some Procedure accepts argument while some does not accept arguments.
(4) Return Type is also optional. If function procedure returns any value then you has to specify return type otherwise there is no need to specify return type.

Return statement is used inside Function Procedure to return value to the point from where it is called.
Function Procedure can be called using two different methods as shown below:

Call FunctionName ([Argument List])
OR
FunctionName [Argument List]

     

Example of Function Procedure


Design an application using Function Procedure to find maximum from two numbers.
Step 1: Design a form as shown below:
Event Procedure
Step 2: Now set properties of various controls as given below:

Control Name Property Name Value

Form1

Text

Max From Two Numbers

Label1

Text

Number1:

Label2

Text

Number2:

TextBox1

Name

txtNumber1

TextBox2

Name

txtNumber2

Button1

Name

cmdMax

Text

Maximum

Label3

Name

lblMax

Text

Maximum Number Is ___

Step 3: Now define a Function procedure named Maximum as given below:

Function Maximum(ByVal a As Integer, ByVal b As Integer) As Integer
If a > b Then
Return a
Else
Return b
End If
End Function

Step 4: Now double click on Maximum Button and write following code to call Function Procedure.

Dim c As Integer
c = Maximum(txtNumber1.Text, txtNumber2.Text)
lblMax.Text = "Maximum Number is " & c

Download Projects


Download Programs