为什么我需要在任何地方声明函数的类型? [英] Why do I need to declare the type of a function everywhere I use it?
问题描述
程序功能测试
隐式无
真正:: 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 use
d 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屋!