Fortran数组不能在函数中返回:不是DUMMY变量 [英] Fortran array cannot be returned in function: not a DUMMY variable

查看:2188
本文介绍了Fortran数组不能在函数中返回:不是DUMMY变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Fortran 90自由形式的新手,我真的很想知道为什么下面这段代码片段不起作用:

 程序test2 
隐式无
!!!练习f90写作的程序。
!定义双精度数据
integer,parameter :: dp = kind(1.d0)
real(dp):: a(3),b(3)
integer :: i
a =(/(i,i = 1,3)/)
b =(/(i,i = 1,3)/)
write(*,*)m31tensorprod(a,b)

包含
函数m31tensorprod(a,b)
real(dp),dimension(3),intent(in):: a,b
real(dp ),intent(out):: m31tensorprod(3,3)
integer :: k1,k2
forall(k1 = 1:3,k2 = 1:3)
m31tensorprod(k1, k2)= a(k1)* b(k2)
结束
返回
结束函数m31tensorprod
结束程序test2

当我尝试通过gfortran test2.f90编译它时,它说:


test2.f90:13.4:

pre $函数m31tensorprod(a,b)
错误:符号at 1)不是DUMMY变量


我以为 m31tensorprod 是一个内部函数它不应该被宣布。

谢谢,

解决方案

你是正确的是 m31tensorprod 是一个内部函数,这意味着你不必在主程序中声明它。在术语中:它有一个明确的接口。



然而,这不是你的代码的问题。功能定义本身出了什么问题。 [诚然,编译器的消息不是太有用。]



函数子程序的定义

  function m31tensorprod(a,b)

结果变量 m31tensorprod 。这个结果变量需要你的声明

  real(dp),intent(out):: m31tensorprod(3,3)

这是不正确的声明。您可以声明类型( real(dp))和维度((3,3)),但 intent(out)是错误的。

intent Fortran标准的内容受制于约束(C538)


具有INTENT属性的实体应该是一个虚拟数据对象或一个虚拟过程指针。

$ b回到编译器消息, m31tensorprod 不是一个虚拟变量。$ b

在这种情况下,伪参数是 a b 。一般来说,虚拟参数是之间的那些东西,


Being new to Fortran 90 free-form, I would really like to know why the following piece of code snippet would not work:

program test2
    implicit none
    !!! A program to practice f90 writing.
    ! Define double precision data
    integer, parameter :: dp = kind(1.d0)
    real(dp) :: a(3), b(3)
    integer :: i
    a = (/(i, i=1, 3)/)
    b = (/(i, i=1, 3)/)
    write (*, *) m31tensorprod(a, b)

contains
    function m31tensorprod(a, b)
        real(dp), dimension(3), intent(in) :: a, b
        real(dp), intent(out) :: m31tensorprod(3, 3)
        integer :: k1, k2
        forall(k1=1:3, k2=1:3)
            m31tensorprod(k1, k2) = a(k1) * b(k2)
        end forall
        return
    end function m31tensorprod
end program test2

When I try to compile this via gfortran test2.f90, it says:

test2.f90:13.4:

function m31tensorprod(a, b)
1 Error: Symbol at (1) is not a DUMMY variable

I thought because m31tensorprod is an internal function, it shouldn't need to be declared. Where did I do wrong?

Thanks,

解决方案

You are correct that m31tensorprod being an internal function means that you do not have to declare it in the main program. In the jargon: it has an explicit interface.

However, that is not the problem with your code. What is going wrong is with the function definition itself. [Admittedly the compiler message isn't too helpful.]

The definition of the function subprogram

function m31tensorprod(a, b)

defines a function with result variable m31tensorprod. This result variable is subject to your declaration

    real(dp), intent(out) :: m31tensorprod(3, 3)

It is this declaration which is incorrect. You may declare type (real(dp)) and dimension ((3,3)) but the intent(out) is erroneous.

The intent attribute, in the words of the Fortran standard, is subject to the constraint (C538)

An entity with the INTENT attribute shall be a dummy data object or a dummy procedure pointer.

Coming back to the compiler message, m31tensorprod is not a dummy variable. In this case the dummy arguments are a and b. In general the dummy arguments are those things between the ( and the ),

这篇关于Fortran数组不能在函数中返回:不是DUMMY变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆