Here is my example code :
main program :
program main
use,intrinsic :: iso_fortran_env,only: int32
use test_pointers
implicit none
integer(kind=int32),target :: number1,number2
number1 = 10;number2 = 30
print "(a,1x,i0)", "The sum is:",sum_of_two(number1, number2)
print "(a,1x,i0)", "The value of number 1 is:",number1
end program main
The module test_pointers:
module test_pointers
use,intrinsic :: iso_fortran_env,only: int32
implicit none
private
public :: sum_of_two
contains
function sum_of_two(a,b) result(two_numbers_added)
integer(kind=int32),pointer,intent(in) :: a,b
integer(kind=int32) :: two_numbers_added
two_numbers_added = a+b
a = a+1_int32
end function sum_of_two
end module test_pointers
Compiled with :
gfortran -O3 test_module.f90 main.f90 -o test_pointer
Output:
The sum is: 40
The value of number 1 is: 11
What is the purpose of intent(in)
here ?
Why does it not prevent the function from changing the value of the target ?
However if I remove the pointer
and the target
attribute, the compiler throws an error at compile time.
test_module.f90:16:4:
16 | a = a+1_int32
| 1
Error: Dummy argument ‘a’ with INTENT(IN) in variable definition context (assignment) at (1)