PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Function returning string - cannot write result

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
mwhite



Joined: 19 Feb 2007
Posts: 5

PostPosted: Wed Feb 21, 2007 4:06 pm    Post subject: Function returning string - cannot write result Reply with quote

Hello,

I have a problem trying to WRITE the result of a function that returns a string. Whenever I try to WRITE the result, I get an error: "PGFIO-F-235/formatted write/internal file/edit descriptor does not match item." However, if I try PRINT, the result is displayed correctly.

Code:

PROGRAM test
   CHARACTER(LEN=4)      :: mambo  !function
   INTEGER                       :: x

   x = 1
   WRITE(*, '(A)') mambo (x)
END PROGRAM test

FUNCTION mambo (x) RESULT (string)
   CHARACTER(LEN=4)        :: string
   INTEGER                         :: x

   WRITE(string, "(I4.4)") x
   RETURN
END FUNCTION mambo


Since x = 1, I would expect the program to print out '0001'. Instead, I get:

Code:

inferno [1739] ./a.out
PGFIO-F-235/formatted write/internal file/edit descriptor does not match item


If I replace "WRITE(*, '(A)') mambo (x)" with "PRINT *, mambo(x)" ... it works as expected.

Any idea what is going on?

Thanks
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 2033
Location: The Portland Group Inc.

PostPosted: Wed Feb 21, 2007 5:45 pm    Post subject: Reply with quote

Hi mwhite,

The statement "WRITE(*, '(A)') mambo (x)" is illegal F95 since mambo contains an WRITE statement. Functions that are part of an output list may not cause execution of another input/output statement. You'll need to create a temporary variable to hold the return value. I believe this restriction has been lifted in the F2003 standard.

- Mat

Example:
Code:
% cat mambo.f90
PROGRAM test
   INTEGER                       :: x
   character(len=4) :: temp
   character(len=4) :: mambo

   x = 1
   temp =  mambo(x)
   WRITE(*, '(A)') temp
END PROGRAM test

FUNCTION mambo (x) result(string)
   CHARACTER(LEN=4)        :: string
   INTEGER                 :: x
   WRITE(string, '(I4.4)') x
   RETURN
END FUNCTION mambo

% pgf90 mambo.f90
% a.out
0001
Back to top
View user's profile
mwhite



Joined: 19 Feb 2007
Posts: 5

PostPosted: Thu Feb 22, 2007 7:49 am    Post subject: Reply with quote

Ah, thanks for the explanation, Mat. Your suggested fix worked perfectly.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group