1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
module help
use iso_c_binding
implicit none
interface
integer function add( a, b ) bind( C, name = "add" ) ! the "name=" bit is only necessary if you want some upper case
import c_int
integer(c_int), value :: a, b ! "value" is necessary for pass by value
end function add
subroutine subtract( a, b, c ) bind( C, name = "subtract" )
import c_int
integer(c_int), value :: a, b ! note that a and b are passed by value
integer(c_int) :: c ! c is passed by reference
end subroutine subtract
subroutine multiply( a, b, c ) bind( C, name = "multiply" )
import c_int
integer(c_int) :: a, b ! everything passed by reference
integer(c_int) :: c
end subroutine multiply
end interface
end module help
!=======================================================================
program test1
use help
implicit none
integer(c_int) a, b
integer(c_int) c, d, e
write( *, * ) "Input a and b"
read( *, * ) a, b
c = add( a, b ) ! calls an external C++ function returning an int
write( *, * ) "Sum is ", c
call subtract( a, b, d ) ! calls an external C++ void function
write( *, * ) "Difference is ", d
call multiply( a, b, e ) ! calls an external C++ void function
write( *, * ) "Product is ", e
end program test1
|