1module add_funs 2implicitnone 3interface add 4moduleprocedure add_real, add_integer, add_3 6endinterface 7contains 8realfunction add_real(a, b) 9implicitnone 10real :: a, b 11 add_real = a + b 12endfunction 13integerfunction add_integer(a, b) 14implicitnone 15integer :: a, b 16 add_integer = a + b 17endfunction 18realfunction add_3(a, b, c) 19implicitnone 20real :: a, b, c 21 add_3 = a + b + c 22endfunction 23endmodule add_funs 24program main 25use add_funs 26implicitnone 27write(*,*) add(1,2) 28write(*,*) add(2.,3.) 29write(*,*) add(1.,2.,3.) 30endprogram main
上述代码中的&符号是用来续行的,新标准中仅需要把续行符放在代码末尾就可以了。
如果仅仅是参数个数不同,还可利用optional属性来限定参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1module add_funs 2implicitnone 3contains 4realfunction add(a, b, c) 5implicitnone 6real :: a, b 7real, optional :: c 8 add = a + b 9if(present(c)) add = add+c 10endfunction 11endmodule add_funs 12program main 13use add_funs 14implicitnone 15write(*,*) add(2.,3.) 16write(*,*) add(1.,2.,3.) 17endprogram main