“Unclassifiable statement” when referencing a func

2020-04-16 03:56发布

问题:

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)

回答1:

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