Overloading functions with Fortran

10,892

Your example is perfectly valid. They can be distinguished by the TYPE of the arguments. The names are not important then. In your case the type of the cur argument differs.

The arguments with the same name can be distinguished by their type, kind or rank (TKR compatibility).

The point of the referenced article is that you cannot distinguished two specific procedures only by the ORDER of the arguments. It is because the procedures can be called with the keyword arguments in any order. This can be overcomed by using different names for the arguments.

Otherwise declaring more specific procedures for a generic one with the same names of arguments, but with different types/kinds/ranks is very common and perfectly valid.

Fortran 2003/2008 adds some more possibilities to generic resolution. It is also possible to distinguish procedures by the pointer/allocatable attribute of their arguments and by the procedure pointer dummy arguments.

Share:
10,892

Related videos on Youtube

alex_reader
Author by

alex_reader

Updated on September 14, 2022

Comments

  • alex_reader
    alex_reader over 1 year

    In Fortran 90, we can overload functions with an interface. However, according to this site, we cannot define these functions with the same arguments name. With gfortran, it does not seem to be a problem as the following code works well enough:

    interface check
      module procedure check_int, check_real
    end interface
    
    contains 
    
    subroutine check_int(cur, dname, func_name, fname)
      integer, allocatable, intent(in) :: cur(:)
      character(*) :: dname, func_name, fname
      ...
    end subroutine
    
    subroutine check_real(cur, dname, func_name, fname)
      real, allocatable, intent(in) :: cur(:)
      character(*) :: dname, func_name, fname
      ...
    end subroutine
    

    So, is it bad practice to do so?

    Edit: Calling the function with keywords does not change anything.