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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return value from lib function 1

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
0
0
DK
Hi,

I', kind of new to Fortran, so I've probably missed something completely obvious...

This is my lib code:
Code:
    real function jMultiply(a,b)
        implicit none
        real a,b
        jMultiply = a*b
    end function jMultiply

    subroutine sMultiply (a,b,res)
        real res,a,b
        res = a*b
    end

which I have compiled into a lib:
Code:
f95   -c -g -w1 -o JFTools.o JFTools.f90
ar rv libjflib.a JFTools.o

Have made this program to use the lib:
Code:
real :: r
r = jMultiply(1.2,1.2)
PRINT*,r
call sMultiply(1.2,1.2,r)
PRINT*,r
END

which I have compiled:
Code:
f95   -o testJFLib.o -L../JFLib/ ../JFLib/libjflib.a

Using SunStudio12.1 on a Ubuntu 9.04 Linux system

The program returns
Code:
0.0E+0
1.44

In other words, the subroutine works fine, but the function does not return anything. I have tried to move the function into the main program - in which case it returns 1.44 as expected. I have also tried to debug the program, and the values are passed correctly, but somehow the result is not returned to the caller - why? help! Thx in advance!





Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Hi,

You should say your compiler that the used function is external.
I added in the test program this
Code:
real :: r
[COLOR=red]real, external :: jMultiply[/color]
r = jMultiply(1.2,1.2)
PRINT*,r
call sMultiply(1.2,1.2,r)
PRINT*,r
END
then I compiled it with g95 (on Windows) so
Code:
C:\msys\1.0\home\mikl1071>g95 -c -g -o JFTools.o JFTools.f90

C:\msys\1.0\home\mikl1071>ar rv libjflib.a JFTools.o
ar: creating libjflib.a
a - JFTools.o

C:\msys\1.0\home\mikl1071>g95 -o testJFLib.exe testJFLib.f90 -L. libjflib.a
and now it runs as expected
Code:
C:\msys\1.0\home\mikl1071>testJFLib.exe
 1.44
 1.44
 
thx mikrom, that works for me too.
it this always necessary when calling functions in libraries? - and why is is not necessary for the subroutine?

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
sunaj said:
it this always necessary when calling functions in libraries? - and why is is not necessary for the subroutine?
I'm not sure, but I think that the compiler needs to know, if the function's result datatype is compatible with the variable in which it should be assigned. So you need to declare the datatype of the function in your program.
With procedures it's not necessary, because they don't return results, only modify their arguments.
I do it always so.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top