Calculates the power of a value, defaults to square : Function Parameter « Language Basics « VB.Net





Calculates the power of a value, defaults to square

Calculates the power of a value, defaults to square
Imports System

Public Class MainClass

    Shared Sub Main(ByVal args As String())
      Dim value As Integer

      ' call version of Power depending on power input
      value = Power(Convert.ToInt32(3), _
           Convert.ToInt32(10))

      Console.WriteLine( Convert.ToString(value) )

    End Sub

   ' use iteration to calculate power
   Shared Function Power(ByVal base As Integer, _
      Optional ByVal exponent As Integer = 2) As Integer

      Dim total As Integer = 1
      Dim i As Integer

      For i = 1 To exponent
         total *= base
      Next

      Return total
   End Function ' Power

End Class

           
       










Related examples in the same category

1.Use Array as Function ParameterUse Array as Function Parameter
2.Pass Array as ParametersPass Array as Parameters
3.Two Dimension Array: Pass into a FunctionTwo Dimension Array: Pass into a Function