Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple Factorial function for VB

Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#1   May 28 '07
Here's a very simple function for VB (written in VB6) to calculate the factorial (!) of a number. Note that it is limited by the magnitude of the value which can be stored in the Long data type. (In fact, I believe it overflows at 13!)

Expand|Select|Wrap|Line Numbers
  1. Public Function Factorial(ByVal bNum As Byte) As Long
  2.   Dim I As Long
  3.   If bNum <= 0 Then Exit Function
  4.   Factorial = 1
  5.   For I = 1 To bNum
  6.     Factorial = Factorial * I
  7.   Next
  8. End Function



Reply


Similar Visual Basic 4 / 5 / 6 bytes