Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to get a simple function to work

Status
Not open for further replies.

TommyCee

Civil/Environmental
Nov 6, 2009
13
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.)
 
Replies continue below

Recommended for you

The answer came to me in my sleep, and I'll post it here:

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 ...
 
I don't know enough about Fortran to really comment, but your interface definition does not appear to be consistent with your function description. In your function description, you specify a return type for your function Integer*4. Nowhere in your interface definition is there a corresponding specification, although you do specify the input variable.

TTFN

FAQ731-376
 
Thanks for your reply, IRstuff. I had thought the answer was as I posted above: a Fortran function cannot call another Fortran function.

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?
 
Recursive function calling is usually a mandatory part of a programming language. Trying to write recursive algorithms without that feature is all but impossible. The only limitation is usually the depth of the call stack.

TTFN

FAQ731-376
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor