2016-10-19 21 views
0

私は何かを本当に簡単にしようとしていますが、モジュールで定義されている関数を呼び出すだけですが、機能していません。モジュールから関数を呼び出す

これは私は、これはここに

testMod.f90:3.4: 

use doubleMod 
    1 
testMod.f90:8.15: 

call double(n) 
       2 
Error: 'double' at (1) has a type, which is not consistent with the CALL at (2) 

コード モジュールでエラーであるLinuxの

gfortran -o testingMOD testMod.f90 doubleMod.f90 

を使用して、それをコンパイルするために行うものです。

module doubleMod 
implicit none 

contains 
function double (n) 
    implicit none 
    integer :: n, double 

    double = 2*n 
    write(*,*) double 
end function double 

end module doubleMod 

ファイルの呼び出しそれ:

program testMod 

use doubleMod 

implicit none 
integer :: n = 3 

call double(n) 

end program testMod 
+2

'call'はサブルーチン、ない機能のためです。 – francescalus

+0

'call double(n)'を 'write(*、*)double(n)'に置き換え、何が起こるかを見てください。 –

+0

@HighPerformanceMarkでは、再帰I/Oが問題になることがあります。したがって、おそらく 'n = double(n)'です。 – francescalus

答えて

1

Fortranには、関数とサブルーチンの2種類の主要なプロシージャがあります。関数は値を返すので、式の中で呼び出されます。例:値を返さない

a = myfunc(b) 
print*, myfunc(a) 

サブルーチンは、彼らが呼び出される必要があります。

call mysub(a, b) 

callに関数をしようとか、式の中でサブルーチンを使用する構文エラーです。あなたのケースでは

、あなたはどちらかはサブルーチンにdoubleを変換することができます:

subroutine double(n) 
    implicit none 
    integer, intent(inout) :: n 
    n = 2 * n 
end subroutine double 

その後doubleへのごcallが倍増するnの値になります。

ORあなたがdoubleを呼び出す方法を変更することができます。

program testMod 
    use doubleMod 
    implicit none 
    integer :: n 
    n = 3 
    n = double(n) 
    print*, n  ! prints 6 
end program testMod 
関連する問題