Skip to content

bindings/examples/fortran/basics.f90

Complete Fortran example. Build with python bindings/fortran/build.py; run the resulting ELF on GaspardOS x64.

Source files · Download basics.f90

program basics
  use iso_c_binding, only: c_double
  implicit none
  integer :: i
  real(c_double) :: values(3), total
  logical :: positive
  character(len=20) :: title

  title = 'Fortran / GaspardOS'
  values = (/ 1.0_c_double, 2.0_c_double, 3.0_c_double /)
  total = 0.0_c_double
  do i = 1, size(values)
    total = total + square(values(i))
  end do
  positive = total > 0.0_c_double
  if (positive) then
    print *, trim(title), ': ', total
  else
    print *, 'Zero'
  end if
  call scale(values, 2.0_c_double)
  print *, values
contains
  function square(x) result(y)
    real(c_double), intent(in) :: x
    real(c_double) :: y
    y = x * x
  end function square

  subroutine scale(x, factor)
    real(c_double), intent(inout) :: x(:)
    real(c_double), intent(in) :: factor
    x = x * factor
  end subroutine scale
end program basics