The intent attribute specifies what a dummy argument's intended use is inside of a function or subroutine.
The intent attribute is used to give the computer information about the intended use of dummy arguments in a subroutine or function. This is accomplished by specifying one of three different intents, so that the computer knows how you are planning on treating your dummy arguments. These three different intent specifications and their meanings are listed below.
This attribute setting tells the compiler that this argument will only be used to pass information into the subprogram. As a consequence of specifying intent(in), the variables that are marked with this designation can not have their values changed inside of the subprogram. A Fortran error will result if a variable declared Intent(in) appears on the left side of an assignment statement.
Intent(out) will tell the computer that whatever dummy arguments are given this attribute can only be used to return information to the calling procedure. The value of any dummy argument given this attribute will be undefined upon entry to the subprogram. A Fortran error will result if a variable declared Intent(out) does not appear on the left side of an assignment statement.
This tells the compiler that these dummy arguments can be used to both receive information from a subprogram and provide information to it.
The following block of code gives a brief example of how the intent attributes can be used inside both the main program and in a subroutine.
Program add_it implicit none real a,b,c interface subroutine addition(x,y,z) implicit none real x,y,z intent (in) x,y intent (out) z end subroutine addition end interface data a,b / 12.1,56.2 / call addition (a,b,c) print *, c stop end
Subroutine addition(x,y,z) implicit none real x,y,z intent(in) x,y intent(out) z z= x + y return end
Please, note two things about this example before you take it to be the only way to use the intent attribute.. First, the dummy arguments can be declared inside the subroutine and interface block like this,
real, intent(in) :: x,y real, intent(out) :: z
instead of the manner in which they are done above. Also, the interface block is not a necessary item. The program would function fine if the intent arguments were used inside of the subroutine only. The interface block is just included to illustrate how extra security check can be added to your program to ensure the proper arguments get used the correct manner.
lecture thirty six
examples: interface.f and dual-interface.f
Written by Jason Wehr: firstname.lastname@example.org and Maintained by John Mahaffy : email@example.com