Fortran pass an array to function

user1543042

I am trying to pass an array of unknown length to a function. I would also prefer the indices of a is the same as b. Is this possible?

The program compiles but does run through the function.

Any help would be appreciated.

 function RealCumSum(i) result(j)
    real, dimension(1:), intent(in)  :: i ! input
    real, dimension(size(i)) :: j ! output
    integer :: m

    do m = 1,size(i)
        j(m) = sum(i(1:m))
    end do

 end function RealCumSum

 program xfunc
    implicit none
    real, dimension(2) :: a = (/ 3.2 , 2.5 /)
    real, dimension(2) :: b, RealCumSum

    b = RealCumSum(a)

    write(*,*) "cumulative sum of ",a," is ", b
 end program xfunc
Vladimir F

The assumed shape array arguments (dimension(:)) require an explicit interface. It is best done by placing the procedure in a module. The other options are to make the procedure internal (using contains) or to supply an interface block.

module m
 implicit none
contains
 function RealCumSum(i) result(j)
    real, dimension(1:), intent(in)  :: i ! input
    real, dimension(size(i)) :: j ! output
    integer :: m

    do m = 1,size(i)
        j(m) = sum(i(1:m))
    end do

 end function RealCumSum
end module

 program xfunc
    use m

    implicit none
    real, dimension(2) :: a = (/ 3.2 , 2.5 /)
    real, dimension(2) :: b, RealCumSum

    b = RealCumSum(a)

    write(*,*) "cumulative sum of ",a," is ", b
 end program xfunc

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive Fortran function return array?

From Dev

Does fortran function pass by const reference?

From Dev

Function in fortran, passing array in, receiving array out

From Dev

Syntax to pass an array to a function

From Dev

Excel - pass an array into a function

From Dev

PHP Array pass to function

From Dev

Pass struct array to function

From Dev

pass the location of a an array in a function

From Dev

pass an array to a function

From Dev

Fortran array cannot be returned in function: not a DUMMY variable

From Dev

Constant function pointer array in Fortran 2003

From Dev

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

From Dev

How to pass single dimension array from fortran to c

From Dev

Pass arrays from C/C++ to Fortran and return a calculated array

From Dev

How to Pass an Array to a Fortran DLL using DLLImport without mangling it in the process?

From Dev

Why are values placed in an array in a fortran function not there in the calling c++ function?

From Dev

Pass array to function name collision

From Dev

How to pass a function an array literal?

From Dev

Pass an array item to a function in PowerShell

From Dev

How to pass an array to a function in VBA?

From Dev

cannot pass array in a function javascript

From Dev

Pass char pointer array to function

From Dev

Pass a dynamic array of objects to function

From Dev

pass associative array into php function

From Dev

Pass a C array to a Rust function

From Dev

How to pass an array as function argument?

From Dev

Pass a PowerShell array to a .NET function

From Dev

How to pass a array into webapi function?

From Dev

Pass in part of an array as function argument

Related Related

HotTag

Archive