Function Fortran:调用函数中的其他函数

Function Fortran:调用函数中的其他函数,function,fortran,gfortran,Function,Fortran,Gfortran,我在代码::Blocks:main.f95,example.f95上的两个独立文件中编写了GNU Fortran代码。main.f95内容: program testing use example implicit none integer :: a, b write(*,"(a)", advance="no") "Enter first number: " read(*,*) a write(*,"(a)", advance="no") "Enter

我在代码::Blocks:main.f95,example.f95上的两个独立文件中编写了GNU Fortran代码。main.f95内容:

program testing

   use example

   implicit none
   integer :: a, b

   write(*,"(a)", advance="no") "Enter first number: "
   read(*,*) a

   write(*,"(a)", advance="no") "Enter second number: "
   read(*,*) b

   write(*,*) factorial(a)
   write(*,*) permutation(a, b)
   write(*,*) combination(a, b)

end program testing
module example


contains


  integer function factorial(x)

     implicit none
     integer, intent(in) :: x
     integer :: product_ = 1, i

     if (x < 1) then

        factorial = -1

     else if (x == 0 .or. x == 1) then

        factorial = 1

     else

        do i = 2, x
           product_ = product_ * i
        end do

        factorial = product_

     end if

  end function factorial


  real function permutation(x, y)

     implicit none
     integer, intent(in) :: x, y
     permutation = factorial(x) / factorial(x - y)

  end function permutation


  real function combination(x, y)

     implicit none
     integer, intent(in) :: x, y

     combination = permutation(x, y) / factorial(y)

  end function combination


end module example
示例.f95内容:

program testing

   use example

   implicit none
   integer :: a, b

   write(*,"(a)", advance="no") "Enter first number: "
   read(*,*) a

   write(*,"(a)", advance="no") "Enter second number: "
   read(*,*) b

   write(*,*) factorial(a)
   write(*,*) permutation(a, b)
   write(*,*) combination(a, b)

end program testing
module example


contains


  integer function factorial(x)

     implicit none
     integer, intent(in) :: x
     integer :: product_ = 1, i

     if (x < 1) then

        factorial = -1

     else if (x == 0 .or. x == 1) then

        factorial = 1

     else

        do i = 2, x
           product_ = product_ * i
        end do

        factorial = product_

     end if

  end function factorial


  real function permutation(x, y)

     implicit none
     integer, intent(in) :: x, y
     permutation = factorial(x) / factorial(x - y)

  end function permutation


  real function combination(x, y)

     implicit none
     integer, intent(in) :: x, y

     combination = permutation(x, y) / factorial(y)

  end function combination


end module example

排列和组合函数不能正常工作。谢谢你的回答。

我认为你与Fortran的一个著名的(对那些知道它的人来说)陷阱相冲突。但在透露之前,我要问你做了多少测试?我运行了你的代码,得到了奇怪的结果,并思考了一分钟

然后我测试了
factorial
函数的
x
的一些小值,它们产生了

 factorial            1  =            1
 factorial            2  =            2
 factorial            3  =           12
 factorial            4  =          288
 factorial            5  =        34560
 factorial            6  =     24883200
 factorial            7  =    857276416
 factorial            8  =   -511705088
 factorial            9  =   1073741824
 factorial           10  =            0
这显然是错误的。因此,在请求帮助之前,您似乎没有正确地测试代码。(我没有测试你的
组合
排列
函数。)

哦,时间,哦,更多

您已经初始化了行中的变量
product

 integer :: product_ = 1, i
这自动意味着
product
获取属性
save
,因此它的值在调用之间存储(明白了!)。在每次调用开始时(第一次调用除外)
产品
具有上次调用结束时的值

补救方法很简单,不要初始化
产品
。改变

 integer :: product_ = 1, i


更简单的方法是不编写自己的阶乘函数,而是使用内在的
函数,但那是另一回事。

我想你已经与Fortran的一个著名的(对那些知道它的人来说)问题相冲突了。但在透露之前,我要问你做了多少测试?我运行了你的代码,得到了奇怪的结果,并思考了一分钟

然后我测试了
factorial
函数的
x
的一些小值,它们产生了

 factorial            1  =            1
 factorial            2  =            2
 factorial            3  =           12
 factorial            4  =          288
 factorial            5  =        34560
 factorial            6  =     24883200
 factorial            7  =    857276416
 factorial            8  =   -511705088
 factorial            9  =   1073741824
 factorial           10  =            0
这显然是错误的。因此,在请求帮助之前,您似乎没有正确地测试代码。(我没有测试你的
组合
排列
函数。)

哦,时间,哦,更多

您已经初始化了行中的变量
product

 integer :: product_ = 1, i
这自动意味着
product
获取属性
save
,因此它的值在调用之间存储(明白了!)。在每次调用开始时(第一次调用除外)
产品
具有上次调用结束时的值

补救方法很简单,不要初始化
产品
。改变

 integer :: product_ = 1, i


更简单的方法是不编写自己的阶乘函数,而是使用内在的
产品
函数,但这是另一回事。

这让许多C/C++程序员感到惊讶
integer::i=42
不等于
integer::i;i=42
,而是保存为整数::i=42。
i
的值在调用之间保持不变,并且从不重置为42。这让许多C/C++程序员感到惊讶
integer::i=42
不等于
integer::i;i=42
,而是保存为整数::i=42。
i
的值在调用之间保持不变,并且从不重置为42。