我可以在Fortran中过早从函数返回值吗? [英] Can I return a value from a function prematurely in Fortran?

查看:37
本文介绍了我可以在Fortran中过早从函数返回值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,我可以轻松地从函数返回值:

In C I can easily return a value from a function:

int foo(int b) {
   if (b == 0) return 42;
   int a;
   // calculate a
   return a;
}

但是在Fortran中, RETURN 语句用于错误处理.我能做

But in Fortran the RETURN statement serves error handling. I could do

integer function foo(b)
    integer :: b, a
    if (b == 0) ! what should I enter here??
    // calculate a
    foo = a
end function

如何在现代Fortran中做到这一点?

How do I do this in modern Fortran?

我知道在这种情况下,只要 if-then-else-endif 就足够了.但是在某些情况下,我不会,而且我也不想举一个过于复杂的例子.

I know that in this case and if-then-else-endif would suffice. But there are cases when it wouldn't and I don't want to make an overly complex example.

推荐答案

在Fortran中,您使用 return 语句退出过程.

In Fortran you use the return statement to exit a procedure.

这篇关于我可以在Fortran中过早从函数返回值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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