- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In your example I would change
procedure(), pointer:: funcPoint => null()
to
procedure(model1), pointer:: funcPoint => null()
![](/skins/images/2E08A100FB92911314A240D1EAFB2828/responsive_peak/images/icon_anonymous_message.png)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page