How to declare the type of a function that returns an array in Fortran?

bcf

I have function that returns an array, say

function f(A)
    implicit none
    real, intent(in) :: A(5)
    real, intent(out) :: f(5)

    f = A+1
end

My question is, how can I define f in the main program unit? E.g.

program main
    implicit none
    real :: A(5)
    real, dimension(5), external :: f  ! does not work

    ...
end 
casey

You need an explicit interface. You can do this in a few ways.

  1. Explicitly in the scoping unit that calls f:

    interface
      function f(A)
        implicit none
        real, intent(in) :: A(5)
        real :: f(5)
      end function
    end interface
    
  2. Place the function in your program host scope as an internal function:

     program main
        ...
     contains
       function f(A)
         implicit none
         real, intent(in) :: A(5)
         real :: f(5)
    
         f = A+1
       end
     end program
    
  3. Place the function in a module:

     module A
     contains
       function f(A)
         implicit none
         real, intent(in) :: A(5)
         real :: f(5)
    
         f = A+1
       end
     end module
    
     program main
       use A
       ...
     end program
    
  4. Use the explicit interface from a different procedure with the same arguments and return type, kind and rank.

    program main
      interface
        function r5i_r5o(r5)
          implicit none
          real, intent(in) :: r5(5)
          real :: r5i_r5o(5)
        end function
      end interface
    
      procedure(r5i_r5o) :: f
      ...
    end program
    
    function f(A)
      implicit none
      real, intent(in) :: A(5)
      real :: f(5)
    
      f = A+1
    end
    

The cleanest way of doing this is option #3 using modules. This gives you the benefit of an automatic explicit interface (not needing to do option #1 everywhere you call f) and makes your function available everywhere the module is used rather than limited to a specific scoping unit as in option #2. Option #4 can be handy if you have many procedures with the same argument and return types since one explicit interface can be re-used for all of them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to declare a complex type array in fortran 90

From Dev

How to declare function that returns an array of object?

From Dev

How to declare a function that returns an array in glsl es (version 100)

From Dev

How to declare the array type in swift

From Dev

How to declare an array with type constraints?

From Dev

How to declare the array type in swift

From Dev

How to generically type a function that returns a value array of an object array?

From Dev

Declare a function in Swift that returns a closure with a generic T.Type

From Dev

How to declare function return type `int (*)[3]`?

From Dev

How to declare function return type `int (*)[3]`?

From Dev

how to declare type of function property returned on object

From Dev

How to declare array of specific type in javascript

From Dev

How to declare an array of rowtype in a PostgreSQL function?

From Dev

C: how to declare a static function that returns a nonstatic string?

From Dev

How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

From Dev

How to assert that a function returns an array

From Dev

How to delete an array that a function returns

From Dev

How can one declare a function using a function type in GO

From Dev

How to declare an array type that can contain either a string or other array?

From Dev

Fortran pass an array to function

From Dev

declare dynamic array in function

From Dev

how to declare an array based on if else condition inside a function and calling this array

From Dev

Fortran 90, how to use array defined in derived type in a subroutine

From Dev

How can I declare the type of a function defined within a let

From Dev

How to declare a Kotlin function with return type 'void' for a java caller?

From Dev

How to explicitly declare a cv::Mat type as part of a OpenCV library function

From Dev

How to declare a variable mid-routine in Fortran

From Dev

How to declare a variable mid-routine in Fortran

From Dev

In Processing.js, how to declare an object type and an array of objects?

Related Related

  1. 1

    How to declare a complex type array in fortran 90

  2. 2

    How to declare function that returns an array of object?

  3. 3

    How to declare a function that returns an array in glsl es (version 100)

  4. 4

    How to declare the array type in swift

  5. 5

    How to declare an array with type constraints?

  6. 6

    How to declare the array type in swift

  7. 7

    How to generically type a function that returns a value array of an object array?

  8. 8

    Declare a function in Swift that returns a closure with a generic T.Type

  9. 9

    How to declare function return type `int (*)[3]`?

  10. 10

    How to declare function return type `int (*)[3]`?

  11. 11

    how to declare type of function property returned on object

  12. 12

    How to declare array of specific type in javascript

  13. 13

    How to declare an array of rowtype in a PostgreSQL function?

  14. 14

    C: how to declare a static function that returns a nonstatic string?

  15. 15

    How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?

  16. 16

    How to assert that a function returns an array

  17. 17

    How to delete an array that a function returns

  18. 18

    How can one declare a function using a function type in GO

  19. 19

    How to declare an array type that can contain either a string or other array?

  20. 20

    Fortran pass an array to function

  21. 21

    declare dynamic array in function

  22. 22

    how to declare an array based on if else condition inside a function and calling this array

  23. 23

    Fortran 90, how to use array defined in derived type in a subroutine

  24. 24

    How can I declare the type of a function defined within a let

  25. 25

    How to declare a Kotlin function with return type 'void' for a java caller?

  26. 26

    How to explicitly declare a cv::Mat type as part of a OpenCV library function

  27. 27

    How to declare a variable mid-routine in Fortran

  28. 28

    How to declare a variable mid-routine in Fortran

  29. 29

    In Processing.js, how to declare an object type and an array of objects?

HotTag

Archive