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 708189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:21:47+00:00 2026-05-14T04:21:47+00:00

I can create function pointers in Fortran 90, with code like real, external ::

  • 0

I can create function pointers in Fortran 90, with code like

real, external :: f

and then use f as an argument to another function/subroutine. But what if I want an array of function pointers? In C I would just do

double (*f[])(int);

to create an array of functions returning double and taking an integer argument. I tried the most obvious,

real, external, dimension(3) :: f

but gfortran doesn’t let me mix EXTERNAL and DIMENSION. Is there any way to do what I want? (The context for this is a program for solving a system of differential equations, so I could input the equations without having a million parameters in my subroutines.)

  • 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-14T04:21:48+00:00Added an answer on May 14, 2026 at 4:21 am

    The declaration “real, external :: f” doesn’t really make “f” into a full pointer since you can’t change the procedure that it points — it does permit you to pass this single function to another routine., So you also need the “pointer” attribute. There are examples on page 267 of “Fortran 95/2003 explained” by Metcalf, Reid & Cohen — a google search for “fortran procedure pointer” will bring up this page. A simple example close to yours is “real, external, pointer :: f_ptr”. Alternatively: “procedure (f), pointer :: f_ptr”. This is a Fortran 2003 feature — http://gcc.gnu.org/wiki/Fortran2003 and http://gcc.gnu.org/wiki/ProcedurePointers lists partial support in gfortran, best with 4.5. I’m not sure whether “dimension” is directly allowed, but you can assign a procedure to a pointer, which provides a lot of flexibility. You can also put the pointer into a derived type, which could be made into an array.

    Edit: here is a code example which works with gfortran 4.5:
    Edit 2: line commented out per comments below.

    module ExampleFuncs
    
      implicit none
    
    contains
    
    function f1 (x)
      real :: f1
      real, intent (in) :: x
    
      f1 = 2.0 * x
    
      return
    end function f1
    
    
    function f2 (x)
       real :: f2
       real, intent (in) :: x
    
       f2 = 3.0 * x**2
    
       return
    end function f2
    
    
    function fancy (func, x)
    
       real :: fancy
       real, intent (in) :: x
    
       interface AFunc
          function func (y)
             real :: func
             real, intent (in) ::y
          end function func
       end interface AFunc
    
       fancy = func (x) + 3.3 * x
    
    end function fancy
    
    end module  ExampleFuncs
    
    program test_proc_ptr
    
      use ExampleFuncs
    
      implicit none
    
      ! REMOVE: pointer :: func
      interface
         function func (z)
            real :: func
            real, intent (in) :: z
         end function func
      end interface
    
      procedure (func), pointer :: f_ptr => null ()
    
      type Contains_f_ptr
         procedure (func), pointer, nopass :: my_f_ptr
      end type Contains_f_ptr
    
      type (Contains_f_ptr), dimension (2) :: NewType
    
    
      f_ptr => f1
      write (*, *) f_ptr (2.0)
      write (*, *) fancy (f_ptr, 2.0)
    
      f_ptr => f2
      write (*, *) f_ptr (2.0)
      write (*, *) fancy (f_ptr, 2.0)
    
      NewType(1) % my_f_ptr => f1
      NewType(2) % my_f_ptr => f2
    
      write (*, *) NewType(1) % my_f_ptr (3.0), NewType(2) % my_f_ptr (3.0)
    
      stop
    
    end program test_proc_ptr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's possible to call C functions through CREATE FUNCTION but how can .NET functions
I would like to create a function that can deal with an object that
I have to create a function that can take in an array of pointers
in C++, I can easily create a function pointer by taking the address of
How can I create an update function in javascript/jquery? An update function is a
How can I create an anonymous and curried function in Scala? The following two
How can I create a buffer in linux kernel without malloc() function (or calloc()
I'm looking for a way to create a function that can be used in
I'm looking to create a PHP function that can trim each line in a
I'm using CreateWindowEx() function to create an EDIT window, i.e. where a user can

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.