Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-25-2014, 01:44 PM   #1
Registered: Mar 2014
Posts: 444

Rep: Reputation: Disabled
GFortran compilation error

Hi there. I am trying to compile a simple Gfortran program with one function in it. I get a single compilation error which I cannot resolve. Please help

!Computation of LP
        program LP
! angle xx is in degrees. It is being converted to radians.
          real*8 :: xx, res
          integer*4 :: ll,mm
          xx = 20.0
          ll = 20
          mm = 5
          res = methodDirect (xx,ll,mm)
800       format (e12.3)
          write (*,800) res
        end program LP

        function methodDirect (xx,ll,mm)
!          xx is the angle in degrees. Has to be converted to radians
           real*8 :: xx, methodDirect
           real*8, parameter :: pi = 3.1415927
           integer*4 :: ll,mm
!          converting to radians:
           xx = xx * pi / 180.0
           methodDirect = xx
        end function methodDirect
The error is:

res = methodDirect (xx,ll,mm)
Error: Return type mismatch of function 'methoddirect' at (1) (INTEGER(4)/REAL(8))
I cannot understand why it claims that I have a mismatch unless I am blind.

Thanks, - Alex

Last edited by AlexBB; 10-25-2014 at 01:50 PM.
Old 10-25-2014, 02:43 PM   #2
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,812

Rep: Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749Reputation: 749
Warning: my last use of Fortran was in some previous century.

methodDirect is not explicitly declared, so being in the range I-N it defaults to integer. You need to declare it real to return a real result.

       real function methodDirect(xx,ll,mm)
Old 10-25-2014, 05:36 PM   #3
Registered: Mar 2014
Posts: 444

Original Poster
Rep: Reputation: Disabled
I tried that before with the same error:

real*8 function methodDirect (xx,ll,mm)
This Cambridge pdf manual in Fortran95 suggests that the function can be declared inside the function body which I also did and it compiled:

function f(x)
   real :: f, x
   f = x**3 + x - 3.0
end function f
Old 10-25-2014, 06:57 PM   #4
Registered: Mar 2014
Posts: 444

Original Poster
Rep: Reputation: Disabled
I finally cracked it. The function must be declared in the main body of the program:

real*8 :: xx, res, methodDirect


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Where is the error in a Gfortran file? AlexBB Linux - Newbie 3 10-19-2014 11:42 AM
[SOLVED] Gfortran compilation error. Why? AlexBB Linux - Newbie 2 05-04-2014 10:33 AM
compilation problem for gfortran surja Programming 2 07-08-2013 07:58 AM
gfortran runtime error saray Linux - Software 1 05-02-2009 10:56 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration