Segmentation fault with optional arguments in Fortran functions

不羁岁月 提交于 2019-12-19 07:06:30

问题


I can use Fortran optional argumenrs with subroutines with intent(in) and intent(inout), but with functions optional arguments work only with intent(in), right? With intent(inout) I get segmentation faults in the following code:

real function foo(x, tol) 
    real, intent(in) :: x
    real, optional, intent(inout) :: tol
    if( .not. present(tol) ) tol = 1e-6
    !...
end function foo

回答1:


I found the problem, I used the variable even when not present on the fourth line (in tol = 1e-6):

real function foo(x, tol) 
    real, intent(in) :: x
    real, optional, intent(inout) :: tol
    if( .not. present(tol) ) tol = 1e-6
    !...
end function foo 

But I would like to use it even when not present and set a default value, like when in C++ we do something like that

double foo(double x, double tol=1e-6)

Unfortunately, it seems it is not possible in Fortran.



来源:https://stackoverflow.com/questions/18452668/segmentation-fault-with-optional-arguments-in-fortran-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!