“不可分类的声明”当引用函数时

发布于 2024-09-18 09:11:43 字数 1360 浏览 8 评论 0原文

我正在学习函数中的虚拟参数和局部变量。

我正在使用的书中的练习之一是编写一个程序,询问用户的名字和姓氏,然后将这些名字连接在一起并打印全名。代码如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-09-25 09:11:43

您在 full_name(first,last) 行上遇到错误 - 尽管它给了我语法错误,但可能只是编译器不同。

您正在使用的函数返回一个值,因此您可以直接在 print 语句中使用它。在此之前不需要使用它,即使在此之前使用过它,您仍然需要将其值(它返回的值)分配给类似

string = full_name(first,last) 的

东西无论如何,我缩短了稍微高一点,就这样吧。

  program name_test
  implicit none

  character(len=12) :: first, last
  character(len=30), external :: full_name

  write(*,'("Please enter your first name : ",\)'); read(*,*)first
  write(*,'("Please enter your last name  : ",\)'); read(*,*)last
  write(*,'("Welcome ",A)')full_name(first,last)

  end program name_test


  function full_name(first,last)
  implicit none

  character(len=*) :: first, last
  character(len=30) :: full_name

  full_name = trim(adjustl(first))//" "//trim(adjustl(last))
  end function full_name

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.

  program name_test
  implicit none

  character(len=12) :: first, last
  character(len=30), external :: full_name

  write(*,'("Please enter your first name : ",\)'); read(*,*)first
  write(*,'("Please enter your last name  : ",\)'); read(*,*)last
  write(*,'("Welcome ",A)')full_name(first,last)

  end program name_test


  function full_name(first,last)
  implicit none

  character(len=*) :: first, last
  character(len=30) :: full_name

  full_name = trim(adjustl(first))//" "//trim(adjustl(last))
  end function full_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文