Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29523 Discussions

error #6341 - ifx can't compile line like: if(aFunction)

cean
New Contributor II
99 Views

Here is a demo.

! if_aFunc.f90
!======================================================================
! Compile with: 

! ifx if_aFunc.f90
!======================================================================
! Compiling Errors:

!Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2025.1.0 Build 20250317
!Copyright (C) 1985-2025 Intel Corporation. All rights reserved.

!if_afunc.f90(27): error #6385: The highest data type rank permitted is INTEGER(KIND=8).   [AFUNC]
!      if (aFunc(sText1)) print *,'false'
!----------^
!if_afunc.f90(27): error #6341: A logical data type is required in this context.   [AFUNC]
!      if (aFunc(sText1)) print *,'false'
!----------^
!compilation aborted for if_afunc.f90 (code 1)
!======================================================================
!source

      logical      :: iRet       
      iRet = aFunc(sText1)
      if (iRet) print *,'Work' 
      
      !Can't compile next line 
      if (aFunc(sText1)) print *,'Not Work.'
      end
!======================================================================
RECURSIVE LOGICAL FUNCTION aFunc()
      aFunc=.TRUE. 
END FUNCTION aFunc



0 Kudos
7 Replies
Steve_Lionel
Honored Contributor III
78 Views

The compiler is correct, because aFunc is implicitly REAL and not allowed as the IF expression. It's possibly also a mismatch in your real application. Try this instead:

      logical      :: iRet, aFunc       
      iRet = aFunc(sText1)
      if (iRet) print *,'Work' 
      
      !Can't compile next line 
      if (aFunc(sText1)) print *,'Not Work.'
      end
cean
New Contributor II
59 Views

why this definition doesn't work: RECURSIVE LOGICAL FUNCTION aFunc()

How to define a function to return a  LOGICAL value?

I am trying to compile Xeffort. It has a lost of this kind of call.

 

0 Kudos
witwald
New Contributor II
53 Views

Here is a working example of your original code. It compiles and runs using the Intel ifx compiler (under Windows). It is necessary to ensure that the data types are explicitly declared. Using 'implicit none' will help you to ensure that this is the case.

 

program prog_if_aFunc

implicit none

logical           :: aFunc, iRet
character(len=20) :: sText1

iRet = aFunc(sText1)
if (iRet) print *,'Works - iRet'

if (aFunc(sText1)) print *,'Works - aFunc'

end

!======================================================================

RECURSIVE LOGICAL FUNCTION aFunc(sText)
  implicit none
  character(len=*), intent(in) :: sText
  aFunc = .TRUE.
END FUNCTION aFunc

 Below is the output:

 Works - iRet
 Works - aFunc
0 Kudos
cean
New Contributor II
49 Views
No, you have the redeclare in the main.
logical :: aFunc

I don't want to add this.
0 Kudos
witwald
New Contributor II
19 Views

The compiler is not clairvoyant when it comes to using functions in a program. In the C programming language, this issue is gotten around by the use of header files. That way the compiler knows how to deal with a function when it encounters it. Of course, most Fortran compilers handle intrinsic functions, which are part of the language standard and are thus well defined, in the manner you are thinking of.

0 Kudos
Steve_Lionel
Honored Contributor III
35 Views

The main program doesn't know the declaration of aFunc as it is a separate subprogram. Just having it in the same source file is not sufficient. Given this, you must declare it in the main program or from whichever program unit you call it from. You can avoid this by putting aFunc in a module and USEing the module in the main program,. or by making aFunc an internal subprogram of the main, after a CONTAINS.

For further reading, see my 2012 post Doctor Fortran Gets Explicit - Again! - Doctor Fortran

cean
New Contributor II
13 Views

Thanks all.

I got this question when trying to compile Xeffort. 

Look at the code, it is in a module. Like the STRCMP function in xftstrings.f90

 

0 Kudos
Reply