Trying to get a simple function to work
Trying to get a simple function to work
(OP)
Here, the "main" program is function WinMain(), and it calls a simple function that "lives" in another *.f90 file (compiled w/ the project. That function is this:
integer*4 function fRun(Nint)
implicit none
integer :: Nint
fRun = Nint + 10
end
An Interface is presented in the declaration area of the calling program:
interface
function fRun(Hint)
integer Hint
end function
end interface
integer*4 :: iResult, Hint
Later, the function is called as follows:
Hint = 2
iResult = fRun(Hint)
The system compiles & links fine. On execution, Hint is set to 2, control goes to the function (in the other f90 file) and Nint takes on the value of 2. fRun becomes 12, as it should.
When control returns to the calling program, Hint still knows what it should be (2) but fRun has lost its mind. Instead of holding a value of 12, its a long, meaningless string (-2147348), which, oddly enough, is minus the value of WinMain (in case that's a clue).
BTW, I tried moving fRun to the bottom of the calling program and I got the same result.
Any ideas as to what I'm missing here?
{No, this is NOT a homework assignment; it's a rudiment of what will become a huge Fortran app.)
integer*4 function fRun(Nint)
implicit none
integer :: Nint
fRun = Nint + 10
end
An Interface is presented in the declaration area of the calling program:
interface
function fRun(Hint)
integer Hint
end function
end interface
integer*4 :: iResult, Hint
Later, the function is called as follows:
Hint = 2
iResult = fRun(Hint)
The system compiles & links fine. On execution, Hint is set to 2, control goes to the function (in the other f90 file) and Nint takes on the value of 2. fRun becomes 12, as it should.
When control returns to the calling program, Hint still knows what it should be (2) but fRun has lost its mind. Instead of holding a value of 12, its a long, meaningless string (-2147348), which, oddly enough, is minus the value of WinMain (in case that's a clue).
BTW, I tried moving fRun to the bottom of the calling program and I got the same result.
Any ideas as to what I'm missing here?
{No, this is NOT a homework assignment; it's a rudiment of what will become a huge Fortran app.)
RE: Trying to get a simple function to work
The clue was, indeed, my comment:
When control returns to the calling program, Hint still knows what it should be (2) but fRun has lost its mind. Instead of holding a value of 12, its a long, meaningless string (-2147348), which, oddly enough, is minus the value of WinMain (in case that's a clue).
There seemed to be possible conflict in functional values. I wondered if, for some crazy reason, Fortran functions cannot call other Fortran functions! I recreated fRun as a subroutine and tested it: it called and performed perfectly - even living in a separate F90 file.
It surprises me that a huge deal of this show-stopping constraint isn't emphasized (or even mentioned) - in Fortran programming documentation/guidance when one looks up "functions". My own Fortran refs. are silent on this issue, and - unfortunately - if I ever learned this when I studied F77 way back when, I forgot it.
Oh well, live & learn ...
RE: Trying to get a simple function to work
TTFN
FAQ731-376: Eng-Tips.com Forum Policies
RE: Trying to get a simple function to work
Your point about the apparent disparity in type specification between my Interface block and fRun itself got me thinking. I could swear that I tried to specify fRun as an integer, either via:
interface
integer function fRun(Hint)
integer Hint
end function
end interface
or as
interface
function fRun(Hint)
integer Hint, fRun
end function
end interface
and the the compilation choked (didn't like the reduncancy). (btw, I'm using Compaq Visual Fortran 6.6C.) So I omitted the spec in the Interface block.
Guess what? I went back into my project and - just for the hell or it - rewrote the Interface block (I tried either way above) and it worked fine !!! The project compiles/builds fine and fRun returns the expected integer to the calling program (f.WinMain). Sweet!
So, I guess I was right in the first place:
A Fortran function can, indeed, call another Fortran function!
Who knew?
RE: Trying to get a simple function to work
TTFN
FAQ731-376: Eng-Tips.com Forum Policies