program series2

!-- compute the terms of the arithmetic progression (s + di) for i = m to n

  implicit none

  print *, series_sum(0, 700, 100.0, 0.1)
  
!-- examples using keyword arguments:  MUST know the dummy argument names
!--   1) all actual arguments prior to the first keyword argument must match
!--      the corresponding dummy argument correctly
!--   2) once a keyword argument is used, then the remaining arguments must use
!--      keywords also

  print *, series_sum(0, 700, d=0.1, s=100.)
  print *, series_sum(n=700, d=0.1, s=100.)
  print *, series_sum(d=0.1, s=100., n=700)
  print *, series_sum(m = 0, n = 700, d = 0.1, s = 100.0)
  
  contains
  
  function series_sum(m, n, s, d) result(series_sum_result)
  
  integer, optional :: m
  integer :: n, i, temp_m
  real :: s, d, series_sum_result

  if (present(m)) then
    temp_m = m
  else
    temp_m = 0
  endif
  
  series_sum_result = 0.0
  do i = temp_m, n
    series_sum_result = series_sum_result + s + i * d
  enddo
  
  end function series_sum
    
end program series2