Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Function return value in IF STATEMENT

Ibrahim_K_
New Contributor I
195 Views

Friends;

This is probably silly question: why does the first if statement gives Compilation error #6385 while the second one compiles.

C
C     LOGICAL :: TEST
C
      IF(COMPASX(RUNTYP,STAT))    GOTO 91
C      
      TEST=COMPASX(RUNTYP,STAT)
      IF(TEST)                    GOTO 91
C

The function COMPASX is defined as follows:

      !-----------------------------------------------------------------
      LOGICAL FUNCTION COMPASX(AONE,ATWO)
      !
      IMPLICIT INTEGER (I-N)
      IMPLICIT REAL (A-H,O-Z)
      !
      !LOGICAL::COMPAS
      CHARACTER,INTENT(IN) :: AONE(4), ATWO(4)
      !
      ! standard Fortran IV convention
      !
      DO I=1,4
          IF( (LLE(AONE(I),ATWO(I)) .XOR. LLE(ATWO(I),AONE(I))) ) THEN
              COMPASX=.FALSE.
              RETURN
          END IF
      END DO
      !
      COMPASX=.TRUE.
      !
      RETURN
      END FUNCTION COMPASX
      
      !-----------------------------------------------------------------

By the way, could somebody suggest a better way to compare two character arrays in FORTRAN?

Regards;

I. Konuk

0 Kudos
5 Replies
andrew_4619
Honored Contributor I
195 Views
You have more than one problem but I think the first one is that the type of function compasx is not known where it is called. If compasx was in a module or there was an explicit interface that would not be the case. Adding logical, external :: compasx in the calling routine would be helpful to the compiler. The types of your call parameters also need examination.
John_Campbell
New Contributor II
195 Views

By the way, could somebody suggest a better way to compare two character arrays in FORTRAN?

The following code fits with your coding approach and is valid Fortran:

  DO I=1,4
    IF ( AONE(I) .EQ. ATWO(I) ) CYCLE
    COMPASX=.FALSE.
    RETURN
  END DO
  COMPASX=.TRUE.
  RETURN

I would suggest that if there is confusion with the order or evaluation of functions in an IF statement (more complex than presented), then your second approach is to be preferred as it removes any ambiguity of order or evaluation when reading the code.

jimdempseyatthecove
Black Belt
195 Views

John,

#3 is not always correct. Depending on character set, one could be defined as having two characters that are lexically equal yet numerically different.

If numerically equal is sufficient, then consider:

!-----------------------------------------------------------------
LOGICAL FUNCTION COMPASX(AONE,ATWO)
  IMPLICIT NONE
  CHARACTER,INTENT(IN) :: AONE(4), ATWO(4)
  INTEGER(4) :: MOLD
  !
  COMPASX = (TRANSFER(AONE,MOLD) == TRANSFER(ATWO,MOLD)) 
  RETURN
END FUNCTION COMPASX

!-----------------------------------------------------------------

Should that function be placed in a module, then chances of inlining are good.

Jim Dempsey

Ibrahim_K_
New Contributor I
195 Views

Dear Friends:

Thank you for the clear explanation (I kind of guessed it) and helpful suggestions. I will use the approach closer to mine but much cleaner.

Regards;

I. Konuk

jimdempseyatthecove
Black Belt
195 Views

FWIW>>Depending on character set, one could be defined as having two characters that are lexically equal yet numerically different.

As an example: 'a' and 'A' could have the same collating sequence (depending on the selected collating sequence).

Jim Dempsey

Reply