为什么我需要在任何地方声明函数的类型? [英] Why do I need to declare the type of a function everywhere I use it?

查看:173
本文介绍了为什么我需要在任何地方声明函数的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 程序功能测试
隐式无
真正:: x,
print *,输入数字到方块
读取(*,*)x
print *,square(x)
结束程序功能测试


实函数square(x)
real :: x
square = x * x
结束函数平方

为什么我需要声明 square real 内部程序functiontest ?我还没有在其定义中声明它是一个真实函数



为什么FORTRAN的作者会这样做设计决定?

解决方案

不,实际上在你的例子中你没有声明它是一个 / code>,但它是该程序的外部函数。如果你在程序中定义了你的函数,如下所示,或者把它放在模块中使用 d it,你并不需要指定它是一个真实函数两次。

 程序functiontest 
implicit none
real :: x
print *,输入一个数字到方块
read(*,*)x
print *,square(x )

包含
实函数square(x)
real :: x
square = x * x
结束函数square
结束程序函数测试

至于为什么它也按照您编写它的方式工作,它是为了向后兼容Fortran 77 。


Consider the following FORTRAN program:

program functiontest
    implicit none
    real :: x, square
    print *, "Enter a number to square"
    read (*,*) x
    print *, square(x)
end program functiontest


real function square(x)
    real :: x
    square = x * x
end function square

Why do I need to declare square to be real inside program functiontest? Haven't I already declared it a real function in its definition?

Why did the authors of FORTRAN make this design decision?

解决方案

No, actually in your example you haven't declared it a real function inside the program, but it's an external function to the program. If you defined your function inside the program, as follows, or put it in a module and used it, you wouldn't have to specify it's a real function twice.

program functiontest
    implicit none
    real :: x
    print *, "Enter a number to square"
    read (*,*) x
    print *, square(x)

contains
    real function square(x)
        real :: x
        square = x * x
    end function square
end program functiontest

As for why it also works the way you wrote it, it is for backwards compatibility with Fortran 77.

这篇关于为什么我需要在任何地方声明函数的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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