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

Procedure pointer implementations using module subroutines

Debanjan_Mukherjee
452 Views

Hello,

I have been trying to implement a procedure pointer based subroutine to do some numerical calculations. What I want to achieve is to be able to select different subroutines based on a choice variable using a select-case construct (each of these subroutines have the same interface). I have reproduced a representative example of this problem below. 

module calculations

 implicit none

 subroutine model1(input1, input2, output)

 end subroutine model1

 subroutine model2(input1, input2, output)

 end subroutine

 subroutine model3(input1, input2, output)

 end subroutine model3

 subroutine calculate(modelChoice, output)

  implicit none

  integer, intent(in):: modelChoice

  double precision, dimension(3), intent(out):: output

  procedure(), pointer:: funcPoint => null()

  select case (modelChoice)

  case(1)

   funcPoint => model1

  case(2)

   funcPoint => model2

  case(3)

   funcPoint => model3

  case default

   funcPoint => model1

  end select

  call funcPoint(1,2,output)

 end subroutine calculate

end module calculations

I want to know whether I am doing this right. I ask this because when I use the procedure pointer implementations this way the input and output arguments don't get passed properly in the code. When I use their names directly then they work fine - which makes me think the subroutines themselves work fine individually. I can present a more verbose example of my code if needed - but can anyone tell me if I am doing this right ?

Thanks

0 Kudos
3 Replies
Steven_L_Intel1
Employee
452 Views

The general flow seems ok. It is critically important that the procedure pointers be declared as having an interface that matches that of the procedures being called. A compilable example would help us help you - I generally find that outlines and paraphrases leave out what's important.

0 Kudos
Debanjan_Mukherjee
452 Views

Would I need to declare an interface in this example ? If so, where would I declare it - in the module specification block, or inside the subroutine that uses the procedure pointers ? 

I will condense and place a compilable example for this on the thread - but I feel as if the interface issue might be where I am going wrong, since I have no delcared interfaces in the code. 

0 Kudos
JVanB
Valued Contributor II
452 Views

In your example I would change

procedure(), pointer:: funcPoint => null()

to

procedure(model1), pointer:: funcPoint => null()

0 Kudos
Reply