Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:08:52+00:00 2026-05-17T17:08:52+00:00

In Fortran, you can pass a function/subroutine A as an argument to another function/subroutine

  • 0

In Fortran, you can pass a function/subroutine A as an argument to another function/subroutine B, but can you store A for later retrieval and use?

for example, this is allowed in C

int foo(float, char, char) { /*whatever*/};

int (*pointerToFunction)(float, char, char);
pointerToFunction = foo;

In Fortran you can pass a subroutine as an argument

subroutine foo
! whatever
end subroutine foo

subroutine bar(func)
    call func
end subroutine bar

program x

    call bar(foo)

end program

but how can you store the address of foo in a similar way to C ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:08:53+00:00Added an answer on May 17, 2026 at 5:08 pm

    Starting from so-called “Fortran 2003” (ISO/IEC 1539-2004) procedure pointers is a part of the Fortran language. It’s definitely of the major new features of Fortran language.

    Usage example from Fortran Wiki.


    Stefano, you mentioned strategy design pattern. In Fortran 2003 you can use pure OOP way to implement it (without procedure pointers). Offhand example:

    strategies.f90

    module strategies
    
      implicit none
    
      private
    
      public :: strategies_transportation_strategy, &
                strategies_by_taxi_strategy, &
                strategies_by_bus_strategy
    
      type, abstract :: strategies_transportation_strategy
      contains
        procedure(transportation_strategy_go), deferred :: go
      end type strategies_transportation_strategy
    
      type, extends(strategies_transportation_strategy) :: strategies_by_taxi_strategy
      contains
        procedure :: go => strategies_by_taxi_strategy_go
      end type strategies_by_taxi_strategy
    
      type, extends(strategies_transportation_strategy) :: strategies_by_bus_strategy
      contains
        procedure :: go => strategies_by_bus_strategy_go
      end type strategies_by_bus_strategy
    
      abstract interface
        subroutine transportation_strategy_go(this)
          import strategies_transportation_strategy
          class(strategies_transportation_strategy), intent(in) :: this
        end subroutine transportation_strategy_go
      end interface
    
      contains
    
        subroutine strategies_by_taxi_strategy_go(this)
          class(strategies_by_taxi_strategy), intent(in) :: this
    
          print *, "We are using taxi."
    
        end subroutine strategies_by_taxi_strategy_go
    
        subroutine strategies_by_bus_strategy_go(this)
          class(strategies_by_bus_strategy), intent(in) :: this
    
          print *, "We are using public transport."
    
        end subroutine strategies_by_bus_strategy_go
    
    end module strategies
    

    vehicles.f90

    module vehicles
    
      use strategies
    
      implicit none
    
      private
    
      public :: vehicles_vehicle, &
                vehicles_taxi, &
                vehicles_bus
    
      type, abstract :: vehicles_vehicle
        private
        class(strategies_transportation_strategy), allocatable :: transportation_strategy
      contains
        procedure :: set_transportation_strategy => vehicle_set_transportation_strategy
        procedure :: go => vehicle_go
      end type vehicles_vehicle
    
      type, extends(vehicles_vehicle) :: vehicles_taxi
      contains
        procedure :: init => taxi_init
      end type vehicles_taxi
    
      type, extends(vehicles_vehicle) :: vehicles_bus
      contains
        procedure :: init => bus_init
      end type vehicles_bus
    
      contains
    
        subroutine vehicle_go(this)
          class(vehicles_vehicle), intent(in) :: this
    
          call this%transportation_strategy%go()
    
        end subroutine vehicle_go
    
        subroutine vehicle_set_transportation_strategy(this, new_transportation_strategy)
          class(vehicles_vehicle), intent(inout) :: this
          class(strategies_transportation_strategy), intent(in) :: new_transportation_strategy
    
          if (allocated(this%transportation_strategy)) then
            deallocate (this%transportation_strategy)
          end if
    
          allocate (this%transportation_strategy, source=new_transportation_strategy)
    
        end subroutine vehicle_set_transportation_strategy
    
        subroutine taxi_init(this)
          class(vehicles_taxi), intent(out) :: this
    
          type(strategies_by_taxi_strategy) :: by_taxi_strategy
    
          call this%set_transportation_strategy(by_taxi_strategy)
    
        end subroutine taxi_init
    
        subroutine bus_init(this)
          class(vehicles_bus), intent(out) :: this
    
          type(strategies_by_bus_strategy) :: by_bus_strategy
    
          call this%set_transportation_strategy(by_bus_strategy)
    
        end subroutine bus_init
    
    end module vehicles
    

    main.f90

    program main
    
      use vehicles
    
      implicit none
    
      type(vehicles_taxi) :: taxi
      type(vehicles_bus) :: bus
    
      call taxi%init()
      call bus%init()
    
      call taxi%go()
      call bus%go()
    
    end program main
    

    At least works using gfortran 4.6 (20100925).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this old Fortran executable that can only be accessed through its GUI,
I have Fortran-based 3D column-major order array flattened into linear memory. What formula/algorithm can
I am not a Fortran programmer (just a short experience), but I need to
This question came about as a result of some mixed-language programming. I had a
My $SHELL is tcsh. I want to run a C shell script that will
I have scripts that have one-liners or sort scripts from other languages within them.
The dot product of two n-dimensional vectors u=[u1,u2,...un] and v=[v1,v2,...,vn] is is given by
Many years ago, C compilers were not particularly smart. As a workaround K&R invented
I have a data table 44 columns wide that I need to write to
Greeting Everyone I'm trying to compile and run a multi-language code in C, C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.