open and write statements in Fortran
I am working through the Fortran tutorial at http://en.wikibooks.org/wiki/Fortran/Fortran_simple_input_and_output. In the following program, what does unit=out_unit do?
program xproduct
implicit none
integer :: i,j
integer, parameter :: out_unit=20
print*,"enter two integers"
read开发者_运维百科 (*,*) i,j
open (unit=out_unit,file="results.txt",action="write",status="replace")
write (out_unit,*) "The product of",i," and",j
write (out_unit,*) "is",i*j
close (out_unit)
end program xproduct
When I run this program, the text file results.txt contains the following text:
The product of 2 and 3
is 6
It specifies the "terminal" to write to. The number contained in out_unit represents the file you opened with the open
statement. If you hadn't used the open
statement and specified the file, output would have been to fort.20
Some terminal numbers have specific meanings. For example, 6 is (usually) stdout, and 5 is (usually) stdin.
In the following program, what does
unit=out_unit
do?
It's using named function parameters.
From Wikipedia:
Named parameters or keyword arguments refer to a computer language's support for function calls that clearly state the name of each parameter within the function call itself.
A function call using named parameters differs from a regular function call in that the values are passed by associating each one with a parameter name, instead of providing an ordered list of values.
精彩评论