dmk34563590
dmk34563590

Reputation: 51

How to pass function in array in fortran?

Method of dealing with symbolic variable function in fortran I have question about passing a defined array into another function. Here is sample code.

!file saved as test5.f08

module test5

    implicit none
    contains

    function f1 (arr1) result (sum_arr)
        real, dimension (:), allocatable, intent (in):: arr1 
        real, dimension (3):: arr2
        real :: sum_arr
        integer :: index

        sum_arr=0
        arr2 = [4,5,6]

        do index= 1,3
            arr1(index) = index  !------Q2-----------
            sum_arr = sum_arr + ( arr1(index)*arr2(index) + arr2(index))
        end do

    end function f1

end module test5

Main program calling above module:

!file saved as test5_call.f08

program test5_call
    use test5
    implicit none

    integer :: n
    real,  allocatable, dimension (:) :: array1
    real::xvar, answer

    allocate (array1(3))
    array1 = [xvar,xvar*2,xvar*xvar] !---------Q1-------

    answer = f1(array1)
    print *,"Answer of array elements  = ", answer

end program test5_call

Compiling as gfortran test5.f08 test5_call.f08 -o test5_call

In the line Q1 in the main program, I am defining an array containing math expression $[x, x^2, X^3]$ I want to pass this array to function f1 in module test5. In line Q2 (in function f1 in module test5), I want to evaluate this array expression. So when index=2, arr1 = [2, 2^2, 2^3].

Basically I want to pass array with (symbolic variable) expression to a function. And evaluate the expression in the function, where the expression will receive values.

This code runs, but there is no effect of array function. Any help how to do this?

I tried searching on this, but didn't get any information. In C, I found a link How do you pass a function as a parameter in C? . As I do not have much knowledge about C, I am not sure whether the technique in the question should be used.

Upvotes: 2

Views: 650

Answers (1)

What your present code is doing:

xvar is just a real number

array1 = [xvar,xvar*2,xvar*xvar] is just an array of three real numbers, the value will depend on the current value of xvar which is undefined in your code.

real, dimension (:), allocatable, intent (in):: arr1 is again just an allocatable array of several real numbers. Note, that you do not need allocatable here at all.


How to do what you intended:

First, you should study how to pass functions at all, see among others

How to pass subroutine names as arguments in Fortran?

That way you can pass one function. I would suggest you to start there and declare your f1 to accept three separate functions

function f1 (func1, func2, func3) result (sum_arr)
  abstract interface
    real function arg_func(x)
      real, intent(in) :: x
    end function
  end interface

  procedure(arg_func) :: func1, func2, func3

Only after you understand how this work and after you are confident using procedure arguments in general you can start thinking about using an array.

You would have to define a container which contains a procedure pointer and use an array of these containers. It is an advanced topic, leave it for the future Function pointer arrays in Fortran

Upvotes: 2

Related Questions