Take the 2-minute tour ×

I am trying to implement a Fortran 90/95/2003 subroutine that does some conditional association of procedure pointers based on a choice parameter. I have abstracted the code that I am building to the following simple example that helps explain my problem:

module mySubroutines
  implicit none
  subroutine modelSine(a_X,a_Y,a_Out1,a_Out2)
    implicit none
    double precision, intent(in):: a_X, a_Y
    double precision, intent(out):: a_Out1, a_Out2
    a_Out1 = sin(a_X)+sin(a_Y)
    a_Out2 = sin(a_X)*sin(a_Y)
  end subroutine modelSine

  subroutine modelCos(a_X,a_Y,a_Out1,a_Out2)
    implicit none
    double precision, intent(in):: a_X, a_Y
    double precision, intent(out):: a_Out1, a_Out2
    a_Out1 = cos(a_X)+cos(a_Y)
    a_Out2 = cos(a_X)*cos(a_Y)
  end subroutine modelCos

  subroutine evaluateModel(modelChoice, a_Out1, a_Out2)
    implicit none
    integer, intent(in):: modelChoice
    double precision, intent(out):: a_Out1, a_Out2
    procedure(), pointer:: modelSub => null()
    if ( modelChoice == 1 ) then
      modelSub => modelSin
    elseif ( modelChoice == 2 ) then
      modelSub => modelCos
      print*,"Wrong input for model choice"
    end if
    call modelSub(1.0d0, 2.0d0, a_Out1, a_Out2)
  end subroutine evaluateModel
end module mySubroutines

I am wondering whether this is the correct way to perform the coding - since I definitely don't get the subroutines associated correctly with the procedure pointers. Any tips would be very helpful.

Thanks Debanjan

share|improve this question
Note that your procedure pointer has an implicit interface. While an explicit interface isn't required with your example usage, there also doesn't appear to be any good reason for not making the interface explicit. Apart from the spelling mistake in the "sin[e]" procedure, what you have works for me. Which compiler and compiler version are you using? –  IanH Mar 21 '13 at 21:36
I am using intel fortran compiler (the composerXE-2011 version). Could you tell me what should I do in terms of making the interfaces explicit ? Is it that I need to define the interface up in the module specification section, or in the 'evaluateModel' subroutine specification section ? –  computanjohn Mar 21 '13 at 22:30
The oldest ifort I have installed is 12.1.7. It works with that version. –  IanH Mar 21 '13 at 22:38
To give a procedure pointer an explicit interface, you place the name of a suitable abstract interface or specific procedure inside the parentheses after the PROCEDURE keyword. –  IanH Mar 21 '13 at 22:39

Your Answer



Browse other questions tagged or ask your own question.