I have a function in a Fortran module with two required arguments and one optional argument. If the optional argument is not present, I would like to set a default value for it within the function.
When I use the function, I get a segmentation fault: Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
The backtrace refers to the first line of
if (.not. present(optionalArg)) then
optionalArg = 2
end if
and the function is defined
function func(reqArg1, reqArg2, optionalArg)
real(kind=real64), intent(in) :: reqArg1
integer, intent(in) :: reqArg2
integer, optional :: optionalArg
real(kind=real64), dimension(reqArg2) :: func
...
Is this the correct way to define a function with an optional argument? I've tried writing an explicit interface for the function both in the module where the other function is called and in the module where the function is defined, but both give me errors. If I put the interface in the module where the function is called I get Error: Cannot change attributes of USE-associated symbol func
since the module is already included with a USE
statement, and if I put the interface in the function's module I get an error about the function already having been defined when the compiler reaches the actual function definition within the CONTAINS
block.