Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在fortran中调用数组值函数?_Arrays_Function_Fortran - Fatal编程技术网

Arrays 如何在fortran中调用数组值函数?

Arrays 如何在fortran中调用数组值函数?,arrays,function,fortran,Arrays,Function,Fortran,我想用fortran编写一个返回可分配数组的函数 program test implicit none real a(3) real, allocatable :: F18(:) a = (/1,2,3/) print *, F18(a) end program test function F18(A) implicit none real A(:) ! An assumed shape array r

我想用fortran编写一个返回可分配数组的函数

program test
    implicit none
    real a(3)
    real, allocatable :: F18(:)
    a = (/1,2,3/)
    print *, F18(a)
end program test

function F18(A)
implicit none
    real A(:)                   ! An assumed shape array
    real F18(size(A,1))         ! The function result itself is
                               ! the second dimension of A.  
    F18 =A                 !  
end function F18
预计会在屏幕上打印“1 2 3”,但我遇到了一个错误:

forrtl:严重(157):程序异常-访问冲突

有什么问题吗

此外,我也尝试过这样的代码:

program test
    implicit none
    real a(3)
    real, allocatable :: F18(:)
    a = (/1,2,3/)
    print *, F18(a,3)
end program test

function F18(A,n)
implicit none
    integer n
    real A(:)                   ! An assumed shape array
    real F18(size(A,1))         ! The function result itself is
                               ! the second dimension of A.  
    F18 =A                 !  
end function F18
在编译过程中,我得到:

Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.4.237 Build 20140805
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

D:\Fortran\Elephant.f90(6): error #6351: The number of subscripts is incorrect.   [F18]
    print *, F18(a,3)
-------------^
compilation aborted for D:\Fortran\Elephant.f90 (code 1)
我真的对fortran的函数感到困惑

在fortran中调用数组值函数的正确方法是什么


@Fortranner

您需要让调用方知道函数的属性。最简单的方法是将其放入一个模块并“使用”该模块。在您的示例中,您在主程序中声明了一个数组“F18”,它不是函数

module mystuff

contains

function F18(A,n)
implicit none
    integer n
    real A(:)                   ! An assumed shape array
    real F18(size(A,1))         ! The function result itself is
                               ! the second dimension of A.
    F18 =A                 !
end function F18


end module mystuff

program test
    use mystuff
    implicit none
    real a(3)
    a = (/1,2,3/)
    print *, F18(a,3)
end program test

谢谢!:),还有一个小问题,我可以使用F18((/1,2,3./),3),但是如果我定义了
real A(:,:)
如何直接传递数组参数?您可以使用内在的
restrape
F18(restrape([1,2,3,4],[2,2])
对于秩1数组,Fortran 2003引入了方括号的使用,
[]
,定义数组构造函数。因此,
a=(/1,2,3/)
可以写成
a=[1,2,3]
。它更容易阅读。