Procedure pointer implementations using module subroutines

Procedure pointer implementations using module subroutines


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)


   funcPoint => model1


   funcPoint => model2


   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 ?


4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Retired 12/31/2016

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. 

In your example I would change

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


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

Leave a Comment

Please sign in to add a comment. Not a member? Join today