      FUNCTION BETA (A, B)
      EXTERNAL GAMMA
      SAVE XMAX, ALNSML
      DATA XMAX, ALNSML /0., 0./
C***FIRST EXECUTABLE STATEMENT  BETA
      IF (ALNSML.NE.0.0) GO TO 10
      CALL GAMLIM (XMIN, XMAX)
      ALNSML = LOG(R1MACH(1))
C
 10   IF (A .LE. 0. .OR. B .LE. 0.) CALL XERMSG ('SLATEC', 'BETA',
     +   'BOTH ARGUMENTS MUST BE GT 0', 2, 2)
C
      IF (A+B.LT.XMAX) BETA = GAMMA(A) * GAMMA(B) / GAMMA(A+B)
      IF (A+B.LT.XMAX) RETURN
C
      BETA = ALBETA (A, B)
      IF (BETA .LT. ALNSML) CALL XERMSG ('SLATEC', 'BETA',
     +   'A AND/OR B SO BIG BETA UNDERFLOWS', 1, 2)
C
      BETA = EXP (BETA)
C
      RETURN
      END
