tags:

views:

67

answers:

2

Just as a forward, I am a complete beginner when it comes to Fortran. I've spent quite awhile looking at the other questions on SO, but I couldn't identify a similar question to this, so please forgive me if my solution is either obvious, or already been answered :)

I'm attempting to learn how to correctly implement a self-written Fortran DLL in a VB.net application. I've been able to have VB recognize the DLL, and execute the function without any errors. The error comes rather as expected output compared to actual output.

My Fortran DLL function reads as follows:

function ex(i) 
   integer*4 i
   ex=i+1 
   return 
end

A very simple function that increments the passed parameter by one and returns the value. (I think). The VB Application has the following code.

<DllImport("ex.dll")> _
Public Shared Function ex(ByRef val As Integer) As Integer
End Function


Private Sub btn_Fortran_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Fortran.Click
   Console.WriteLine(ex(1))
End Sub

So, I'm passing the ex function the integer value 1. So I would expect the value 2 to be written to the console. Instead, I get the value "1073741824" Not exactly equal. Any ideas where I'm obviously falling short?

+2  A: 

Learning a language in a mixed language content is "a hard row to hoe". Note that value you obtained is 2**30.

In the fortran portion, you should also declare the return value of the function: "integer*4 function ex (i)" is the old fashioned way. You are probably getting ex as a real through implicit typing. It is a very good idea to include "implicit none" in all of your programs and procedures to prevent implicit typing. Many compilers include an option for the same purpose.

Late edit: Here is a program that demonstrates what was happening by showing what value is obtained when the bit-pattern real value 2.0 is interpreted as an integer. First the program equates a real and an integer. In this case the compiler "knows" about the types and converts the value. In the second case the raw bit pattern is transferred without being converted.

program test_tc

   real :: my_real
   integer :: my_int

   my_real = 2.0

   my_int = my_real
   write (*, *) my_int

   my_int = transfer ( my_real, my_int )
   write (*, *) my_int

end program test_tc

Output is:

           2
  1073741824
M. S. B.
A: 

It appears that I was nearly on the right track, but the way in which I declared 'i' made some weird things happen. When using the following convention of

integer*4 :: ex, i

The function returns the correct value. So, my function looks like this

function ex(i) 
   integer*4 :: ex, i
   ex=i+1
   return 
end function

Thanks both of you for the help. I upvoted both of you for simply opening my eyes to some aspect of the language I didn't fully understand beforehand.

AndyPerfect
The weird things weren't from the declaration of "i", but the lack of a type declaration for "ex", which became real by default. So the return value was the bit-pattern for the real value 2, which was then interpreted as an integer.
M. S. B.