fortran过程中的save属性

fortran过程中的save属性一般有两种方式
1. 在子程序或函数中添加save关键字
代码如下:
Program testsave
    implicit none
    integer :: i, n = 10
    
    do i = 1, n
        call sub( i )
    end do
    
End program testsave
    
    
Subroutine sub( a )
    implicit none 
    integer    :: a
    real, save :: s

    s = s + a
    print*, s
End subroutine sub

2. 在过程中声明的同时赋初值
代码如下:
Program testsave
    implicit none
    integer :: i, n = 10
    
    do i = 1, n
        call sub( i )
    end do
    
End program testsave
    
    
Subroutine sub( a )
    implicit none 
    integer    :: a
    real       :: s = 0.0

    s = s + a
    print*, s
End subroutine sub

执行结果如下:
   1.000000
   3.000000
   6.000000
   10.00000
   15.00000
   21.00000
   28.00000
   36.00000
   45.00000
   55.00000

 

你可能感兴趣的:(FortranNote,Fortran教程,fortran)