Hi everyone, I have a quick question: How do you use optional arguments?
I often have the following pattern, I have an optional argument, that if not present is taken as some default value. But how do you usually implement this mechanism? I give you the two options below
subroutine some_sub(arg1, arg2)
integer :: arg1
integer, optional :: arg2
integer :: arg_int
!> Option 1
arg_int = 1
if (present(arg2)) arg_int = arg2 ! here potentially move_alloc for big arrays
!> Option 2
if (present(arg2)) then
arg_int = arg2 ! here potentially move_alloc for big arrays
else
arg_int = 1
end if
! Rest of the code
! [...]
end subroutine
Clearly, this example has only scalars involved, so I doubt the two options have differences. However, if you have big arrays the two might differ, because option one would fill the array the first just to re-fill it.
What’s your best-practice/experience?
Cheers, Francesco