In Fortran, we can specify intent(in), intent(out) for the arguments of function/subroutine, and this can help preventing bug, and possible help the compiler to do optimization.
I learned from kargl in Fortran discourse that (unfortunately his comments disappeared), and i can only remember the things like,
subroutine f(x,y)
real, intent(in) :: x
real, intent(out) :: y
integer :: i
do i=1,10
y = sin(x)
enddo
return
end
intent(in) means x is basically ‘read-only’, its value cannot be changed in the subroutine. While intent(out) means y’s value can be changed.
Besides doing some protection for arguments, they might be some performance gain.
I cannot explain very well why, but I remember kargl said something like, adding intent(in) for x, and intent(out) for y, and realizing that sin is a pure function, it definitely can help the compiler to realized that it should just calculate
y=sin(x)
Only once. If I do not add intent(in) for x and intent(out) for y, there is a possibility that the compiler will do really do the stupid loop 100 times.
In Fortran, intent property is optional, but it is encouraged to use them whenever possible. There are obviously more deep reason that Fortran committee design the intent property, but I am not capable of explain it.
Anyway, just curious, in Julia is there similar stuff that can do some protection on the arguments (prevent the values of some arguments being accidently changed by mistake)?
Thanks in advance!