Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Calling functions

Status
Not open for further replies.

GerritGroot

Technical User
Nov 3, 2006
291
ES
Hi,

Using gfortran, if I have a code like

Code:
[COLOR=purple]PROGRAM[/color] [COLOR=blue]Test[/color]
[COLOR=purple]IMPLICIT NONE[/color]
[COLOR=purple]REAL[/color] :: [COLOR=blue]y,MyFunc[/color]
y=[COLOR=blue]MyFunc[/color](2.)
[COLOR=purple]WRITE[/color](*,*)y
[COLOR=purple]END PROGRAM[/color] [COLOR=blue]Test[/color]

[COLOR=purple]REAL FUNCTION[/color] [COLOR=blue]MyFunc[/color](x)
[COLOR=purple]REAL, INTENT[/color](IN) :: x
MyFunc=x**2
[COLOR=purple]END FUNCTION[/color] [COLOR=blue]MyFun[/color]

that works and another one like

Code:
[COLOR=purple]PROGRAM[/color] [COLOR=blue]Test[/color]
[COLOR=purple]IMPLICIT NONE[/color]
[COLOR=purple]REAL[/color] :: [COLOR=blue]MyFunc[/color]
[COLOR=purple]WRITE[/color](*,*)[COLOR=blue]MyFunc[/color](2.)
[COLOR=purple]END PROGRAM[/color] [COLOR=blue]Test[/color]

[COLOR=purple]REAL FUNCTION[/color] [COLOR=blue]MyFunc[/color](x)
[COLOR=purple]REAL, INTENT[/color](IN) :: x
MyFunc=x**2
[COLOR=purple]END FUNCTION[/color] [COLOR=blue]MyFun[/color]

That doesn't work. What can be the cause?
(In the example they both work btw, but the second option in my code causes gfortran's executable to get stuck in a sort of endless loop)

Thanks,

Gerrit

(My reaction may be slow this week, lots of meetings and so on..)
 
Seems like it has to do with recursive input/output. Not sure how well this is supported by gfortran. Your simple example works with gfortran. Can you post some example which can reproduce the "... in a sort of endless loop
 
BTW, which version of gfortran creates the problem? Did u enable optimization?
 
First of all, there is a typo at the end of the function....the function name's got a missing 'c'.

Second, What happens if you enclose your function call in a set of parenthesis, you know, the one after the WRITE statement? Does it make a difference?

I use g95 and the program works just fine...but I seem to recall some problem along time ago, either in fortran or something else and I thought I had something to do with evaluation on the spot, where I couldn't "write(*,*) myfun(3)+3" but I could "write(*,*) (myfun(3)+3)" and I figure the parenthesis were helping in including another evaluation layer BEFORE handing over the result to the write statement.

my 2 cents
 
Your program is perfect if you don't use IO statement within the function.

It exits other limitations with non pure function, like modifying a global variable itself used in the write statement calling the function, but this is rather rare.

François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top