j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
178 stars 15 forks source link

Allow procedure calls to be annotated with INTENTs? #203

Open Beliavsky opened 3 years ago

Beliavsky commented 3 years ago

Some languages, such as Python and Matlab/Octave, let you return multiple entities from a function, for example

mean, sd = stats(x)

In Fortran you can use a subroutine to do this,

call stats(x,mean,sd)

but it is not clear from the calling code what the inputs and outputs are. I suggest that Fortran be extended to allow procedure calls to be annotated by the INTENTs of the procedure. Then you could write

call stats(in: x, out: mean, out: sd)

to call

subroutine stats(x,mean,sd)
real, intent(in) :: x(:)
real, intent(out) :: mean,sd
...
end subroutine stats

You could only annotate with in: inout: or out: if the corresponding procedure argument had the same declaration. For subroutines with many arguments, I often write a comment saying what the outputs are, but assertions checked by the compiler would be better. Maybe annotating procedure calls could help the compiler better optimize the code, since it would know what variables are not changed by the procedure call.

klausler commented 3 years ago

Why not use well-named keyword arguments?

nshaffer commented 3 years ago

Why not use well-named keyword arguments?

Calling library code. This would be really great for calling LAPACK for example, where most routines return results by overwriting inputs.