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

preprocessor in fortran function #ifdef and #endif (Compiler:Compag Visual Fortran 6.6)

dingyzh1982
Beginner
2,486 Views

I get a fortran module, which rewrite below:
but when I compile this code , there are a error exist, I have open the "/fpp" and " /fpp:"/m" ",can we use the preprocessor (ex: #ifdef) in the function routine?, my OS is Windows XP, and Compiler: CVF 6.6, the error is below:
Error: Unrecognized token '#' skipped

the code text is below:

!module_ex.f90

module module_ex
contains
function Fun1(a &
#ifdef EOF
,b &
#endif
)result(c)

implicit none
real,intent(in) :: a
#ifdef EOF
real,intent(in) :: b
#endif
real :: c

c = a**2
#ifdef EOF
c = a**2+b
#endif

end function Fun1
end module module_ex

! main_program.f90

#define EOF
program main_program
use module_ex
implicit none
real a,b,c

a = 1
b = 2

c = Fun1(a &
#ifdef EOF
,b &
#endif
)
print *, c
end program main_program

0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
2,486 Views
Quoting - dingyzh1982

I get a fortran module, which rewrite below:
but when I compile this code , there are a error exist, I have open the "/fpp" and " /fpp:"/m" ",can we use the preprocessor (ex: #ifdef) in the function routine?, my OS is Windows XP, and Compiler: CVF 6.6, the error is below:
Error: Unrecognized token '#' skipped

the code text is below:

!module_ex.f90

module module_ex
contains
function Fun1(a &
#ifdef EOF
,b &
#endif


I think that #ifdef works only on statement level. Try instead:

#ifdef EOF
function FUN1(a, b) result(c)
#else
function FUN1(a) result(c)
#endif

However, in this particular case, it is much better to take advantage of Fortran optional arguments:

[cpp]function Fun1(a,b) result(c)

real,intent(in) :: a
real,OPTIONAL,intent(in) :: b
real :: c
c=a**2
if (PRESENT(b)) then
c = c+b
end if[/cpp]



0 Kudos
jimdempseyatthecove
Honored Contributor III
2,486 Views

The major "fly in the ointment" is you are assuming

use module_ex

is conditionaly compiled with your main_program.

Modules (unless they are part of the same source file) are compiled independently from the source files that use them. Therefore, the #define EOF in main_program is not seen during the compilation of module_ex.

You can use the optional argument(s) technique as Jugoslav suggested or you can use the generic interface to have two different functions in module_ex be selected while using the same named function with differing sets of arguments.

[cpp]interface Fun1
  function Fun1_no_EOF(a) result(c)
    implicit none
    real, intent(in) :: a
    real :: c
  end function Fun1_no_EOF
  function Fun1_EOF(a,b) result(c)
    implicit none
    real, intent(in) :: a, b
    real :: c
  end function Fun1_EOF
end interface Fun1


[/cpp]

Then write the bodies of the funcitons in the module_ex

The #define EOF can be used in main_program which triggers call to Fun1(a,b) or Fun1(a). And these in turn end up calling Fun1_EOF(a,b) or Fun1_no_EOF(a) as the case may be.

The other route is write the two functions without the interface and then use

#ifdef EOF
#define Fun1(a,b) Fun1_EOF(a,b)
#else
#define Fun1(a) Fun1_no_EOF(a)
#endif

Then use Fun1(a) or Fun1(a,b) throught the program with or without conditionals.

--or--

#ifdef EOF
#define Fun1(a) Fun1_EOF(a,CommonEOFargHere)
#else
#define Fun1(a) Fun1_no_EOF(a)
#endif

Then use Fun1(a) throught the program without conditionals

Note, FPP is case sensitive, FORTRAN is not

Your Fortran code could be required to use one of: FUN1, fun1, Fun1
You may have to create a #define for each

Jim Dempsey
0 Kudos
Reply