| 我想写一个函数,共有三个参数: 
 [Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode function func(a, b, c) result(output)
real(8), intent(in):: a, b, c
real(8), intent(inout):: output
...
end function func如今我传了三个整数给a、b、c,程序报错:Type mismatch in argument passed INTEGER(4) to REAL(8)
 实例如下:
 
 [Fortran] syntaxhighlighter_viewsource syntaxhighlighter_copycode program main
    implicit none
    real, allocatable:: array(:)
    array = linspace(0.,1.,11)
    print '(*(f3.1,/))', array
    contains
        function linspace(arg1, arg2, N) result(output)
            implicit none
            real:: arg1, arg2
            integer:: N
            real(8),dimension(N):: output
            integer:: i
            output(1) = arg1
            if (N>1) then
                do i = 2, N
                    output(i) = arg1 + (i-1)*(arg2-arg1)/(N-1)
                end do
            end if
        end function
end我看了我们网站的教程:为什么要在数学函数前面加D,加C加Q?如dsin - Fortran教程 - Fortran Coder 程序员聚集地 (fcode.cn)
 了解到 Generic Procedure 可以实现统一接口的功能。
 
 但如果 a、b、c 为单精度呢?为2字节的整型呢?...参数类型组合有十几种都不止。
 难道要为每一中组合都写一个函数吗?
 
 除了 Generic Procedure,有没有其它处理办法?比如在传递参数时进行类型转换。
 
 
 |