user8675309
user8675309

Reputation: 181

Overloading a subroutine and function in Fortran

Is it possible to overload a subroutine and function using the interface blocks? So one can call a procedure via a function or subroutine. For example:

...
interface TestRoutine
      module procedure TestRoutine_fun
      module procedure TestRoutine_sub
end interface TestRoutine

Contains

function TestRoutine_fun(....) result(...)
...
end function TestRoutine_fun

subroutine TestRoutine_sub(...)
...
end subroutine TestRoutine_sub
...

When I tried to compile code similar to this I got the error: Error: In generic interface 'TestRoutine' at (1) procedures must be either all SUBROUTINES or all FUNCTIONs. Thanks in advance!

Upvotes: 1

Views: 993

Answers (2)

Alexander Vogt
Alexander Vogt

Reputation: 18098

You cannot overload a function with a subroutine. What you can do, though, is write a wrapper routine that calls the function (or the other way round). This way, you would at least save some code.

Upvotes: 2

IanH
IanH

Reputation: 21431

No, not in the standard language. The error message explains the situation correctly.

Upvotes: 5

Related Questions