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

How can i define a global parameter which value is defined by a function

史_建鑫
Beginner
1,356 Views

Hello! I want to ask a question about parameter.

Things start from IVF windows application, I want to define a Windows Message to communicate between process. So I use the function RegisterWindowMessage and add a piece of code to handle the message in the MainWndProc. However, according to MFC tradition, I add a case branch in the MainWndProc, but the case value must be a const. So I define the message to be global parameter in the ***global.f90, but i cannot do that. here is the statement in the ***global.f90

[fortran]
integer(UINT), parameter, public :: wm_Message = RegisterWindowMessage("083DDE42-5A00-4DAA-9C84-FAC23F8427D2")
[/fortran]

obviously, it cannot be compiled. Because
1. fortran parameter cannot use the return value of a function as the value. But C++ could
2. module statement can not contain invoke function code.

How can i define a global parameter which value is defined by a function?

Thank you!

0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,356 Views

You can't do it this way. a PARAMETER constant is a compile-time constant.  What you want is a module variable that you set at the start of execution with the result of the function call.

0 Kudos
史_建鑫
Beginner
1,356 Views

Hi Steve~

1.How can I define a module variable that I set at the start of execution with the result of the function call.

2. in c++, i can define a constant whose value defined by the start of execution with the result of the function call.

const UINT wm_Message = RegisterWindowMessage(_T("083DDE42-5A00-4DAA-9C84-FAC23F8427D2"));

you mean I cannot do the same thing in Fortran?

Thank you!

0 Kudos
Steven_L_Intel1
Employee
1,356 Views

Fortran does not have an equivalent of that C++ feature. Fortran allows only a "constant expression" when providing an initial value for a variable.

What I was suggestion was something like this:

[fortran]
module my_globals
use IFWINTY, only: UINT
implicit none
private
public :: wm_Message, initialize_globals

integer(UINT), PROTECTED :: wm_Message

contains
subroutine initialize_globals
use user32
! Initialize all global variables
wm_Message = RegisterWindowMessage("083DDE42-5A00-4DAA-9C84-FAC23F8427D2"C)
end subroutine initialize_globals
end module my_globals
...
program myprog
use my_globals
...
call initialize_globals
... rest of program
[/fortran]

Any place you want to reference wm_Message you would need to add "use my_globals" to make it visible.

The public and private statements make sure that only the specific names listed in the public statement are visible to users of the module. The PROTECTED attribute prevents code outside the module from changing its value. You would add the call to initialize_globals at the start of the program.

0 Kudos
Reply