VB.Net Programming Tutorial on VB.Net Functions

a procedure is a group of statements that together perform a task when called. after the procedure is executed, the control returns to the statement calling the procedure. vb.net has two types of procedures −

  • functions

  • sub procedures or subs

functions return a value, whereas subs do not return a value.

defining a function

the function statement is used to declare the name, parameter and the body of a function. the syntax for the function statement is −

[modifiers] function functionname [(parameterlist)] as returntype
   [statements]
end function

where,

  • modifiers − specify the access level of the function; possible values are: public, private, protected, friend, protected friend and information regarding overloading, overriding, sharing, and shadowing.

  • functionname − indicates the name of the function

  • parameterlist − specifies the list of the parameters

  • returntype − specifies the data type of the variable the function returns

example

following code snippet shows a function findmax that takes two integer values and returns the larger of the two.

function findmax(byval num1 as integer, byval num2 as integer) as integer
   ' local variable declaration */
   dim result as integer
   
   if (num1 > num2) then
      result = num1
   else
      result = num2
   end if
   findmax = result
end function

function returning a value

in vb.net, a function can return a value to the calling code in two ways −

  • by using the return statement

  • by assigning the value to the function name

the following example demonstrates using the findmax function −

module myfunctions
   function findmax(byval num1 as integer, byval num2 as integer) as integer
      ' local variable declaration */
      dim result as integer
      
      if (num1 > num2) then
         result = num1
      else
         result = num2
      end if
      findmax = result
   end function
   sub main()
      dim a as integer = 100
      dim b as integer = 200
      dim res as integer
      
      res = findmax(a, b)
      console.writeline("max value is : {0}", res)
      console.readline()
   end sub
end module

when the above code is compiled and executed, it produces the following result −

max value is : 200

recursive function

a function can call itself. this is known as recursion. following is an example that calculates factorial for a given number using a recursive function −

module myfunctions
   function factorial(byval num as integer) as integer
      ' local variable declaration */
      dim result as integer
      
      if (num = 1) then
         return 1
      else
         result = factorial(num - 1) * num
         return result
      end if
   end function
   sub main()
      'calling the factorial method
      console.writeline("factorial of 6 is : {0}", factorial(6))
      console.writeline("factorial of 7 is : {0}", factorial(7))
      console.writeline("factorial of 8 is : {0}", factorial(8))
      console.readline()
   end sub
end module

when the above code is compiled and executed, it produces the following result −

factorial of 6 is: 720
factorial of 7 is: 5040
factorial of 8 is: 40320

param arrays

at times, while declaring a function or sub procedure, you are not sure of the number of arguments passed as a parameter. vb.net param arrays (or parameter arrays) come into help at these times.

the following example demonstrates this −

module myparamfunc
   function addelements(paramarray arr as integer()) as integer
      dim sum as integer = 0
      dim i as integer = 0
      
      for each i in arr
         sum += i
      next i
      return sum
   end function
   sub main()
      dim sum as integer
      sum = addelements(512, 720, 250, 567, 889)
      console.writeline("the sum is: {0}", sum)
      console.readline()
   end sub
end module

when the above code is compiled and executed, it produces the following result −

the sum is: 2938

passing arrays as function arguments

you can pass an array as a function argument in vb.net. the following example demonstrates this −

module arrayparameter
   function getaverage(byval arr as integer(), byval size as integer) as double
      'local variables
      dim i as integer
      dim avg as double
      dim sum as integer = 0
      
      for i = 0 to size - 1
         sum += arr(i)
      next i
      avg = sum / size
      return avg
   end function
   sub main()
      ' an int array with 5 elements '
      dim balance as integer() = {1000, 2, 3, 17, 50}
      dim avg as double
      'pass pointer to the array as an argument 
      avg = getaverage(balance, 5)
      ' output the returned value '
      console.writeline("average value is: {0} ", avg)
      console.readline()
   end sub
end module

when the above code is compiled and executed, it produces the following result −

average value is: 214.4