murphy@pur-phy (William J. Murphy) (02/28/89)
I was trying to program a function yesterday similar to the following: type complex = Record x, y : real; End; function Complex_Add( z1, z2 : complex) : complex; Begin Complex_Add.x := z1.x + z2.x; Complex_Add.y := z1.y + z2.y; End; var z1, z2, z3 : complex; Begin z1.x := 1.0; z1.y := 2.0; z2.x := 0.0; z2.x := 1.5; z3 := Complex_Add( z1, z2); End. I found out that Turbo Pascal 5.0 does not support returning a record as a value. Does this imply that a function can only return a single value? Is there any way to get a function to return a record? Any help would be appreciated. I will in the mean-time write this with pointers. Thanks, Bill Murphy murphy@newton.physics.purdue.edu
andru@rhialto.SGI.COM (Andrew Myers) (03/01/89)
In article <2012@pur-phy>, murphy@pur-phy (William J. Murphy) writes: > > I was trying to program a function yesterday similar to the following: [program excised] > > I found out that Turbo Pascal 5.0 does not support returning a record as a > value. Does this imply that a function can only return a single value? > Is there any way to get a function to return a record? > Any help would be appreciated. I will in the mean-time write this with > pointers. This is actually in accordance with ANSI Standard Pascal, which specifies that function return values may *only* be simple types (integer,reals,chars,booleans), enumerated types, or pointer types. Basically, anything that fits in a word, and isn't a record. Whether this is a change from TP3,4 I don't know. If you want your Pascal programs to be portable, don't use record types for return values! Andrew
fargo@pawl.rpi.edu (Ethan M. Young) (03/01/89)
It's not that Turbo Pascal won't allow you to return a record, it's that your method was slightly off. Try this: FUNCTION Complex_Add(z1,z2 : complex) : comples; VAR temp : complex; { Temporary variable } BEGIN temp.x := z1.x + z2.x; temp.y := z1.y + z2.y; Complex_Add := temp; END; The method you were using wouldn't work as the function returns a record, but is not itself a record. Thus, using the '.' operator on it is invalid. Thank you and happy hunting! Internet: fargo@pawl.rpi.edu ____ [> SB <] fargo@{paraguay|uruguay}.acm.rpi.edu /__ -=>??<=- Bitnet (??): usergac0@rpitsmts.bitnet / ARGO : 3000 years of regression from the year 4990
murphy@pur-phy (William J. Murphy) (03/01/89)
In article <790@rpi.edu> fargo@pawl.rpi.edu (Ethan M. Young) writes: >It's not that Turbo Pascal won't allow you to return a record, it's that your >method was slightly off. Try this: > >FUNCTION Complex_Add(z1,z2 : complex) : comples; > >VAR > temp : complex; { Temporary variable } > >BEGIN > temp.x := z1.x + z2.x; > temp.y := z1.y + z2.y; > Complex_Add := temp; >END; > >The method you were using wouldn't work as the function returns a record, but >is not itself a record. Thus, using the '.' operator on it is invalid. Though I understand what you are getting at, a posting just previous to yours points out that the function may not be anything other than simple types and pointers. So what that means is that your solution is still not a solution. In Fact, before posting this, I tried what you suggested with Turbo Pasacl 4.0 and it correctly barfs and gags on the declaration of the function as complex. The error message for this problem correctly points out that you cannot declare a function as returning anything other than simple types and pointers. In light of the conversation in this group a few weeks ago about splitting the group into PC/Turbo Pascal and others, I wished to get an answer to this question (above) in the broader scope of knowledge about Pascal. The only Pascal compiler I use is Turbo Pascal. Bill Murphy murphy@newton.physics.purdue.edu
schuetz@iraul1.ira.uka.de (Elmar Schuetz) (03/03/89)
In article <2012@pur-phy> murphy@newton.physics.purdue.edu.UUCP (William J. Murphy) writes: >I found out that Turbo Pascal 5.0 does not support returning a record as a >value. Does this imply that a function can only return a single value? I did never use Turbo Pascal, but as I remember it won't work using any other compiler. Try to return a pointer to this record ... Cheers, Elmar -- Most of all, you've got to hide it from the kids. -- Simon & Garfunkel "Mrs. Robinson"
bobd@ihf1.UUCP (Bob Dietrich) (03/04/89)
In article <2016@pur-phy> murphy@newton.physics.purdue.edu.UUCP (William J. Murphy) writes: > [...] The error message for this problem >correctly points out that you cannot declare a function as returning >anything other than simple types and pointers. > >In light of the conversation in this group a few weeks ago about splitting >the group into PC/Turbo Pascal and others, I wished to get an answer >to this question (above) in the broader scope of knowledge about Pascal. >The only Pascal compiler I use is Turbo Pascal. > >Bill Murphy >murphy@newton.physics.purdue.edu In standard Pascal the statement above is correct: you can only define functions to return simple types (integer, char, real, Boolean, subranges, enumerations) and pointers. To some degree, this was done to keep implementation of the language simple; it was also influenced by the fact for the first implementation of Pascal (CDC 6000 series), all these quantities fit in one 60-bit word. Draft Proposed Extended Pascal relaxes this restriction, and allows anything except files. Thus you can now have functions returning complex (which, BTW, is a simple type!), arrays, records, or sets. usenet: uunet!littlei!intelhf!ihf1!bobd Bob Dietrich or tektronix!ogccse!omepd!ihf1!bobd Intel Corp., Hillsboro, Oregon or tektronix!psu-cs!omepd!ihf1!bobd (503) 696-2092
mikej@pyr1.acs.udel.EDU (Michael Jacobs) (03/04/89)
>>FUNCTION Complex_Add(z1,z2 : complex) : comples; > >Though I understand what you are getting at, a posting just previous >to yours points out that the function may not be anything other than >simple types and pointers. So what that means is that your solution Then declare Type Complex_Number = Record X, Y : Real End; Complex = ^Complex_Number; Procedure Complex_Allocate ( Var C : Complex ); Begin New ( C ) End; Function Complex_Add ( A, B : Complex ) : Complex; ---- Everytime you want to use a complex number, you'd have to create a record and pointer for it (using Complex_Allocate ( perhaps even use a FreeList algorithm for it ) ) and in Complex_Add, two already existing pointers at sent in (or some VERY serious barfing will take place) and a new third on is returned. Another procedure and two more functions will be needed: Procedure Load_Complex ( x,y : real; var C : Complex ); Function Real_Part ( C : Complex ) : Real; Function Imaginary_Part ( C : Complex ) : Real; OR! using your original implementation of the type Procedure Complex_Add ( A, B : Complex; Var C : Complex ); add 'em up and stick 'em in C. That should work. The first thing I talked about with the pointers and billions of functions and procedures is a lot more formal and will probably get more better grades in CIS theory/programming classes, more the second was is alot easier. Mike J, The Grey Sysop... | Ancient Spirits of Evil, transform this Temporal Hitchhiker | decayed form into Mumm-ra, the Ever Living! mikej@vax1.acs.udel.EDU |
markh@csd4.milw.wisc.edu (Mark William Hopkins) (03/06/89)
In article <2012@pur-phy> murphy@newton.physics.purdue.edu.UUCP (William J. Murphy) writes: > >I found out that Turbo Pascal 5.0 does not support returning a record as a >value. Does this imply that a function can only return a single value? >Is there any way to get a function to return a record? I know of three way to do it: (1) Have the function return a pointer to the record, (2) Use a procedure with var parameters, (3) Rewrite the Pascal compiler (and get ANSI to modify the standard). Pascal is otherwise limited in this respect.