Fortran Transition Parameters Using Brackets Prevent Changes

In this question, I asked that the method explicitly prohibited passing passed arguments. Obvious solutions are defining copies of the arguments and using an algorithm for those copies. However, in the comment, I pointed out that I could call the function and wrap an argument that I did not want to change in brackets. This will have the same effect as creating a copy of these passed variables so that it does not change. But I don’t understand how it works and what the brackets do. So can someone explain this to me?

Here is a simple example where behavior occurs as I described.

  1 program argTest                                                                 
  2   implicit none                                                                 
  3   real            ::  a, b, c                                                   
  4                                                                                 
  5   interface       !optional interface                                                              
  6     subroutine change(a,b,c)                                                    
  7       real           ::  a, b, c                                                
  8     end subroutine change                                                       
  9   end interface                                                                 
 10                                                                                 
 11   write(*,*) 'Input a,b,c: '                                                    
 12   read(*,*) a, b, c                                                             
 13                                                                                 
 14   write(*,*) 'Values at start:'                                                 
 15   write(*,*)'a:', a                                                             
 16   write(*,*)'b:', b                                                             
 17   write(*,*)'c:', c                                                             
 18                                                                                 
 19                                                                                 
 20   call change((a),b,c)                                                          
 21   write(*,*)'Values after calling change with brackets around a:'               
 22   write(*,*)'a:', a                                                             
 23   write(*,*)'b:', b                                                             
 24   write(*,*)'c:', c                                                             
 25                                                                                 
 26                                                                                 
 27   call change(a,b,c)                                                            
 28   write(*,*)'Values after calling change without brackets:'                     
 29   write(*,*)'a:', a                                                             
 30   write(*,*)'b:', b                                                             
 31   write(*,*)'c:', c                                                             
 32                                                                                 
 33 end program argTest                                                             
 34                                                                                 
 35                                                                                 
 36 subroutine change(a,b,c)                                                        
 37   real           ::  a, b, c                                                    
 38                                                                                 
 39   a = a*2                                                                       
 40   b = b*3                                                                       
 41   c = c*4                                                                       
 42                                                                                 
 43 end subroutine change                                                           
 44      
 45
 46                                                              
+4
3

, , , ,...

(a) - , a. , , . Fortran , cos(a) . , (a) a, , OP.

Fortran , , , a (a) ,

(a) = some_value

, .

@IanH, , .

+3

(a), , . . a.

(a) a , : - , . , , , Fortran .

- - , . . , " 2", " 1 + 1".

, , .

change , (a), . .

, , ( ) - . , , , , , change , , a change , a .

-, , .

real :: tmp_a
...
tmp_a = a
call change(tmp_a, b, c)
+4

() loc() , :

program main
    implicit none
    integer :: a

    a = 5
    print *, "address(a) = ", loc( a )

    call sub( 100 * a )
    call sub( 1 * a   )
    call sub( 1 * (a) )
    call sub( (a)     )
    call sub( a       )
contains

subroutine sub( n )
    integer :: n
    n = n + 1
    print "(2(a,i4,3x),a,i18)", "a=", a, " n=", n, "address(n) =", loc( n )
end subroutine

end program

, , , , sub() ( ).

# gfortran-6
 address(a) =       140734780422480
a=   5    n= 501   address(n) = 140734780422468
a=   5    n=   6   address(n) = 140734780422464
a=   5    n=   6   address(n) = 140734780422460
a=   5    n=   6   address(n) = 140734780422456
a=   6    n=   6   address(n) = 140734780422480

# ifort-16
 address(a) =        140734590990224
a=   5    n= 501   address(n) = 140734590990208
a=   5    n=   6   address(n) = 140734590990212
a=   5    n=   6   address(n) = 140734590990216
a=   5    n=   6   address(n) = 140734590990220
a=   6    n=   6   address(n) = 140734590990224

# Oracle fortran 12.5
address(a) =  6296328
a=   5    n= 501   address(n) = 140737477281416
a=   5    n=   6   address(n) = 140737477281420
a=   5    n=   6   address(n) = 140737477281424
a=   5    n=   6   address(n) = 140737477281428
a=   6    n=   6   address(n) =         6296328

(, - Oracle a... .)

[] , , ( = , ). , , , , , (...), a.

+2

Source: https://habr.com/ru/post/1661324/


All Articles