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.