“不可分类的声明”当引用函数时
我正在学习函数中的虚拟参数和局部变量。
我正在使用的书中的练习之一是编写一个程序,询问用户的名字和姓氏,然后将这些名字连接在一起并打印全名。代码如下:
PROGRAM name_test
IMPLICIT NONE
! Declare variables
CHARACTER(LEN=12) :: first, last
CHARACTER(LEN=30), EXTERNAL :: full_name
! 1. Ask for first name and family name, then read them
PRINT *, "Please enter your first name"
READ *, first
PRINT *, "Please enter your family name"
READ *, last
! 2. Join names together
full_name(first, last)
! 3. Print welcome message
PRINT *, "Welcome ", full_name(first, last)
END PROGRAM name_test
CHARACTER(LEN=*) FUNCTION full_name(first_name, last_name)
IMPLICIT NONE
! Function which joins 2 names to form a full name
! Dummy argument declarations
CHARACTER(LEN=*), INTENT(IN) :: first_name, last_name
! Local variables
CHARACTER(LEN=LEN(first_name)) :: new_first_name
CHARACTER(LEN=LEN(last_name)) :: new_last_name
! Use ADJUSTL to remove redundant leading blanks
new_first_name = ADJUSTL(first_name)
new_last_name = ADJUSTL(last_name)
! Join names
full_name = TRIM(new_first_name)//" "//new_last_name
END FUNCTION full_name
当我尝试编译时,它出现一个错误,涉及第 15 行的函数调用:
full_name(first, last)
这是编译错误:
Error: Unclassifiable statement at (1)
I'm learning about dummy arguments and local variables in functions.
One of the exercises in the book I'm using is to write a program which asks the user for their first name and last name, then joins the names together and prints the full name. Here's the code:
PROGRAM name_test
IMPLICIT NONE
! Declare variables
CHARACTER(LEN=12) :: first, last
CHARACTER(LEN=30), EXTERNAL :: full_name
! 1. Ask for first name and family name, then read them
PRINT *, "Please enter your first name"
READ *, first
PRINT *, "Please enter your family name"
READ *, last
! 2. Join names together
full_name(first, last)
! 3. Print welcome message
PRINT *, "Welcome ", full_name(first, last)
END PROGRAM name_test
CHARACTER(LEN=*) FUNCTION full_name(first_name, last_name)
IMPLICIT NONE
! Function which joins 2 names to form a full name
! Dummy argument declarations
CHARACTER(LEN=*), INTENT(IN) :: first_name, last_name
! Local variables
CHARACTER(LEN=LEN(first_name)) :: new_first_name
CHARACTER(LEN=LEN(last_name)) :: new_last_name
! Use ADJUSTL to remove redundant leading blanks
new_first_name = ADJUSTL(first_name)
new_last_name = ADJUSTL(last_name)
! Join names
full_name = TRIM(new_first_name)//" "//new_last_name
END FUNCTION full_name
When I try to compile, it comes up with an error referring to the function call at line 15:
full_name(first, last)
This is the compile error:
Error: Unclassifiable statement at (1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 full_name(first,last) 行上遇到错误 - 尽管它给了我语法错误,但可能只是编译器不同。
您正在使用的函数返回一个值,因此您可以直接在 print 语句中使用它。在此之前不需要使用它,即使在此之前使用过它,您仍然需要将其值(它返回的值)分配给类似
string = full_name(first,last) 的
东西无论如何,我缩短了稍微高一点,就这样吧。
You have an error on line full_name(first,last) - although it gives me syntax error, it may be just that the compilers are different.
The function you're using is returning a value, therefore you can use it directly in the print statement. There is no need for it to be used before that, and even if it was used before that, you would still need to assign its value (the one it returned) to something like
string = full_name(first,last)
Anyways, I shortened it up a little so here you go.