1、程序分段:一个程序由一个或若干个程序单位组成。主程序和每一个子程序分别是一个独立的程序单位。子程序是实现模块化的有效方法。
2、每一个程序单位都是以“END”语句结束。主程序段中的END语句作用是使程序“停止运行”;子程序段中的END语句是“使流程返回调用程序”。
3、一个程序有且仅有一个主程序段,可以有0个或若干个子程序段。
4、一个程序单位包括若干行。FORTRAN行分为两类:即语句行和非语句行。
其中语句行由一个FORTRAN语句组成。FORTRAN语句又可分为执行语句和非执行语句。非语句行,即为注释行。
5、FORTRAN程序的基本成份是语句。一行只能有一个语句。若一个语句太长,一行内写不下,可分写在几行上,但要用“续行标志”。
6、FORTRAN程序中语句允许有标号,以便被其他句引用。
7一个程序单位中各类语句的位置是有一定规则的。一般是非执行语句放在所有可执行语句之前。[2]
主要使用下面的三个函数实现:第一个:
COMMAND_ARGUMENT_COUNT — Get number of command line arguments
Description:
COMMAND_ARGUMENT_COUNT returns the number of arguments passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Inquiry function
Syntax:
RESULT = COMMAND_ARGUMENT_COUNT()
Arguments:
None
Return value:
The return value is an INTEGER of default kind.
Example:
program test_command_argument_count
integer :: count
count = command_argument_count()
print *, count
end program test_command_argument_count
COMMAND_ARGUMENT_COUNT — Get number of command line arguments
Description:
COMMAND_ARGUMENT_COUNT returns the number of arguments passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Inquiry function
Syntax:
RESULT = COMMAND_ARGUMENT_COUNT()
Arguments:
None
Return value:
The return value is an INTEGER of default kind.
Example:
program test_command_argument_count
integer :: count
count = command_argument_count()
print *, count
end program test_command_argument_count
第二个:
GET_COMMAND_ARGUMENT — Get command line arguments
Description:
Retrieve the NUMBER-th argument that was passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])
Arguments:
NUMBER Shall be a scalar of type INTEGER and of default kind, NUMBER \geq 0
VALUE (Optional) Shall be a scalar of type CHARACTER and of default kind.
LENGTH (Optional) Shall be a scalar of type INTEGER and of default kind.
STATUS (Optional) Shall be a scalar of type INTEGER and of default kind.
Return value:
After GET_COMMAND_ARGUMENT returns, the VALUE argument holds the
NUMBER-th command line argument. If VALUE can not hold the argument, it
is truncated to fit the length of VALUE. If there are less than NUMBER
arguments specified at the command line, VALUE will be filled with
blanks. If NUMBER = 0, VALUE is set to the name of the program (on
systems that support this feature). The LENGTH argument contains the
length of the NUMBER-th command line argument. If the argument retrieval
fails, STATUS is a positive numberif VALUE contains a truncated
command line argument, STATUS is -1and otherwise the STATUS is zero.
Example:
PROGRAM test_get_command_argument
INTEGER :: i
CHARACTER(len=32) :: arg
i = 0
DO
CALL get_command_argument(i, arg)
IF (LEN_TRIM(arg) == 0) EXIT
WRITE (*,*) TRIM(arg)
i = i+1
END DO
END PROGRAM
GET_COMMAND_ARGUMENT — Get command line arguments
Description:
Retrieve the NUMBER-th argument that was passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])
Arguments:
NUMBER Shall be a scalar of type INTEGER and of default kind, NUMBER \geq 0
VALUE (Optional) Shall be a scalar of type CHARACTER and of default kind.
LENGTH (Optional) Shall be a scalar of type INTEGER and of default kind.
STATUS (Optional) Shall be a scalar of type INTEGER and of default kind.
Return value:
After GET_COMMAND_ARGUMENT returns, the VALUE argument holds the
NUMBER-th command line argument. If VALUE can not hold the argument, it
is truncated to fit the length of VALUE. If there are less than NUMBER
arguments specified at the command line, VALUE will be filled with
blanks. If NUMBER = 0, VALUE is set to the name of the program (on
systems that support this feature). The LENGTH argument contains the
length of the NUMBER-th command line argument. If the argument retrieval
fails, STATUS is a positive numberif VALUE contains a truncated
command line argument, STATUS is -1and otherwise the STATUS is zero.
Example:
PROGRAM test_get_command_argument
INTEGER :: i
CHARACTER(len=32) :: arg
i = 0
DO
CALL get_command_argument(i, arg)
IF (LEN_TRIM(arg) == 0) EXIT
WRITE (*,*) TRIM(arg)
i = i+1
END DO
END PROGRAM
第三个:
GET_COMMAND — Get the entire command line
Description:
Retrieve the entire command line that was used to invoke the program.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND([COMMAND, LENGTH, STATUS])
Arguments:
COMMAND (Optional) shall be of type CHARACTER and of default kind.
LENGTH (Optional) Shall be of type INTEGER and of default kind.
STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:
If COMMAND is present, stores the entire command line that was used to
invoke the program in COMMAND. If LENGTH is present, it is assigned the
length of the command line. If STATUS is present, it is assigned 0 upon
success of the command, -1 if COMMAND is too short to store the command
line, or a positive value in case of an error.
Example:
PROGRAM test_get_command
CHARACTER(len=255) :: cmd
CALL get_command(cmd)
WRITE (*,*) TRIM(cmd)
END PROGRAM
GET_COMMAND — Get the entire command line
Description:
Retrieve the entire command line that was used to invoke the program.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND([COMMAND, LENGTH, STATUS])
Arguments:
COMMAND (Optional) shall be of type CHARACTER and of default kind.
LENGTH (Optional) Shall be of type INTEGER and of default kind.
STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:
If COMMAND is present, stores the entire command line that was used to
invoke the program in COMMAND. If LENGTH is present, it is assigned the
length of the command line. If STATUS is present, it is assigned 0 upon
success of the command, -1 if COMMAND is too short to store the command
line, or a positive value in case of an error.
Example:
PROGRAM test_get_command
CHARACTER(len=255) :: cmd
CALL get_command(cmd)
WRITE (*,*) TRIM(cmd)
END PROGRAM
最近在Fortran的数据传递设计上碰到了问题。现在略归纳下子程序间参数传递的实现办法。
首先,Fortran subroutine
独立地拥有属于自己的变量申明。有说:Fortran的变量作用域只有两类:一是全局作用域,二是子程序作用域,并且子程序内部不存在嵌套作用域。最后一
点很重要哟。举例:在一个子程序里申明的变量,不能在与之并列的另外一个子程序里面使用。即两个子程序不能有变量重叠。即使在这个两个子程序里都使用了同
名的某个变量,本质上讲,在这两个子程序里的同名变量也是没任何联系的。
那么,如果需要在子程序之间传递变量(参数),怎么办呢?还要细说一点:传参的结构有两类:第一类,诸如主程序-子程序、外层子程序-内层子程序(子程序嵌套)之间的参数传递,另外一类,就是并列的两个子程序(即它们之间不需要有关系,仅仅由于需求相同的变量)之间的参数传递。
第一类传参结构的实现常见传址调用。
第二类传参结构的实现常见申明公共内存块,将需求的相同变量申明成全局变量,那么各个子程序自然都可以用了。注意,仍然是地址,所以子程序可以修改这些全局变量的。
其中,FORTRAN77的公共块(全局变量申明)使用COMMONFORTRAN90以后使用MODULE来包装公共块。
来看几个问题:
一、以数组A中满足某一条件的元素的编号构成新的数组B
do bi=1,bn
do ai=1,an
if(A(ai).eq.a0) then
B(bi)=ai
endif
enddo
enddo
二、以数组A的元素为编号,取数组B的元素进行其他运算
do bi=1,bn
do ai=1,an
if (bi.eq.A(ai)) then
fun(i)=fun(i)+fun2(bi)
endif
enddo
enddo
大致思路就是只要不把他们放在一个主程序内就OK了,放在同一个文件或者不同.f文件都是一样的。对于第二个问题,回答也是可以的比如说这样的程序调用:
PROGRAM
MAIN
CALL
SUB1
END
SUBROUTINE
S
CALL
SUB2
END
SUBROUTINE
SUB2()
CALL
SUB3
END
SUBROUTINE
SUB3()
*********
END
这几个子程序和主程序PROGRAM
MAIN可以放在同一个.f文件里面编译,也可以分别放置不同.f文件,编译时效果一样。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)