Optional parameter passed as non-optional argument

A subroutine uses an optional parameter without verifying that it is present.

In this case, the specific error is that the optional parameter is passed to another subroutine as a non-optional argument. This usage pattern is unsafe, as it will not execute correctly when the optional formal parameter is not present and the second call is executed.

This same kind of error can also happen when a FORTRAN dummy argument of type subroutine is invoked. That is, the subroutine that is invoked through a dummy argument might exhibit the same problem as can occur in a direct call. In this case, the problem may or may not happen depending on what subroutine was passed to the dummy argument of subroutine type. There will be an additional observation, in such cases, that identifies the call site where the subroutine argument was passed in.

You can pass an optional parameter as an argument as long as the receiving subroutine expects to receive an optional argument in that position. You can also pass an optional parameter as an argument if you have first verified that the optional parameter is present.

ID

Observation

Description

1

Definition

Definition of the optional parameter argument

Example

          
subroutine mysub2(j)
integer, optional :: j
if (present(j)) print *, j
end

subroutine mysub1(j)
integer :: j
print *,j
end

subroutine icallyou(sub,i)
integer, optional :: i
interface
    subroutine sub(j)
    integer :: j
    end subroutine sub
end interface
call sub(i)
! optional dummy argument "i" as actual argument corresponds
! to nonoptional dummy argument "j" in actual "mysub1"
end

external mysub1, mysub2
interface
    subroutine icallyou(sub,i)
    external sub
    integer, optional :: i
    end subroutine icallyou
end interface

call icallyou(mysub1,5)
call icallyou(mysub1)
call icallyou(mysub2,5)
call icallyou(mysub2)
end        
        

Copyright © 2010, Intel Corporation. All rights reserved.