我必须回答自己这个问题的事实是疯狂的,但事实就是如此。
不同之处在于您不能在 Fortran 中“像其他语言一样调用函数”。在 C 中,您可以在不分配值的情况下调用整数函数,例如
int foo() {
return 5;
}
int main() {
foo(); // this works
}
在 Fortran 中,您总是必须关联一个接收变量。例子
module test
implicit none
contains
integer function foo()
print *, "hello"
foo = 0
end function
end module
program hello
use test
integer :: x
x = foo() ! this works
foo() ! this does not compile
end program hello
这意味着通过返回一个虚拟整数来“模拟”一个 void 函数仍然不允许您在没有接收器变量的情况下调用。
在 Fortran 中,void
返回类型不存在。从技术上讲,你可以用所有函数来构建你的程序,用上面看到的替换call
语句的每个出现x =
,但这不会使你的语法类似于 C 或其他语言,其中没有返回 void 的函数和非无效返回函数。子例程是唯一允许“返回 void”的实体,但执行调用的语义完全不同。除此之外,两者没有区别。