Fortran Wiki
Questions

This page is for general questions about Fortran. If you’re searching for something, such as a library or routine to perform a particular task, feel free to ask here.


Q: Hi! I’m quite new in Fortran, and I’m using Microsoft Visual 2008 as IDE and Intel Fortran as compiler. I’d like to know how I can create a Fortran library using them and how I can link my code to the library. Thanks.


Q: Hi! I have a quite simple question: if I have an array of integers as A = (/1,2,3,4,5/), how do I convert this array in the number 12345? Thank you very much in advanced for your help Regards

A: There are a number of ways you could go about doing this. Perhaps the simplest is to create a loop over the array index and use integer arithmetic operations.

number = 0
DO i=1,SIZE(A)
    number = number + A(i)*10**(SIZE(A)-i)
END DO

Note that I have not tested this to confirm it’s correctness, but it should be correct. If not, you could easily figure out where I made a typo or went wrong. One could also write the array elements to a character variable and then read the character variable back as an integer.


Q: Does anyone know how to overload intrinsic functions? Preferably using f95 compliant syntax if possible. Thanks.

A: It’s actually simple. You just overload the function you want. For example, you can overload ABS:

module myabs

implicit none

type vector
   real :: x, y, z
end type vector

interface ABS
   module procedure absvector
end interface ABS

contains

   real function absvector(vec)
   
      type(vector), intent(in) :: vec

      absvector = sqrt(vec%x ** 2 + vec%y ** 2 + vec%z ** 2)

   end function absvector
 
end module myabs

If you test this, you’ll see that you can now call ABS with REAL, INTEGER, COMPLEX, and even vector.


Q: I am new to Fortran and want to compile a source file. But it gives me a lot of errors:

evfit.f90(47): error #5082: Syntax error, found IDENTIFIER 'UNIT' when expecting one of: ( % [ : . = =>
C         unit conversion
----------^
evfit.f90(47): error #6404: This name does not have a type, and must have an explicit type.   [C]
C         unit conversion
^
evfit.f90(47): error #6404: This name does not have a type, and must have an explicit type.   [CONVERSION]
C         unit conversion
---------------^
evfit.f90(134): error #5120: Unterminated character constant
c      emin = energia all'equilibrio, ottenuta minimizzando chi**2
-------------------------^
evfit.f90(134): error #5144: Invalid character_kind_parameter. No underscore
c      emin = energia all'equilibrio, ottenuta minimizzando chi**2
------------------------------------------------------------------^
evfit.f90(134): error #5082: Syntax error, found IDENTIFIER 'EMIN' when expecting one of: ( % [ : . = =>
c      emin = energia all'equilibrio, ottenuta minimizzando chi**2
-------^
evfit.f90(167): remark #8291: Recommended relationship between field width 'W' and the number of fractional digits 'D' in this edit descriptor is 'W>=D+7'.
      if(istat.eq.1) write(iun,'('' Equation of state: '', &
------------------------------------------------------------^
evfit.f90(169): remark #8291: Recommended relationship between field width 'W' and the number of fractional digits 'D' in this edit descriptor is 'W>=D+7'.
      if(istat.eq.2) write(iun,'('' Equation of state: '', & 
-------------------------------------------------------------^
evfit.f90(171): remark #8291: Recommended relationship between field width 'W' and the number of fractional digits 'D' in this edit descriptor is 'W>=D+7'.
      if(istat.eq.3) write(iun,'('' Equation of state: '', &
------------------------------------------------------------^
evfit.f90(173): remark #8291: Recommended relationship between field width 'W' and the number of fractional digits 'D' in this edit descriptor is 'W>=D+7'.
      if(istat.eq.4) write(iun,'('' Equation of state: '', &
------------------------------------------------------------^
evfit.f90(198): error #5082: Syntax error, found IDENTIFIER 'MINIMIZATION' when expecting one of: ( % [ : . = =>
c      minimization through random seek followed by ZXSSQ (imsl). Input:
-------^
evfit.f90(199): error #5082: Syntax error, found IDENTIFIER 'FUNC' when expecting one of: ( % [ : . = =>
c      func, called by ZXSSQ, produces a vector Deltaf(npt)
-------^

Does anyone know how to handle this?

A: First of all, it’s good practice to include source code when asking questions about errors like this. A minimal program that gives the same error is most helpful. In this case, however, I think there is enough code printed in the above compiler output to tell that the source is in Fixed form layout, which you can see from the comment markers (c and C) in the first columns of some lines. It looks like your compiler is reading the statements in those commented lines, so it is probably treating the file as if it were in Free form layout. Perhaps you can use a compiler flag to indicate which version of Fortran it should use (probably FORTRAN 77), or else change the file extension to .f if it’s not already. –Jason Blevins, August 12, 2013.


Q: A long, long time ago I learned some Fortran IV. I vaguely remember that it was possible to overload functions using the ENTRY keyword. If my memory doesn’t betrays me, every entry point should have the same name of the function, but different argument lists. I am not sure if the return type was also taken into account. I downloaded the Fortran 66 manual linked in this site but found nothing about ENTRY.

Thanks beforehand kind enthusiast Fortran wizards.


Linked questions:


category: help