pascalvax

Returning a string from a Function (VAX PASCAL)


This is one for the software archaeologists!

And before you ask why was I even bothering to try to get this to work the reason it is simply because I can - which I think is a perfectly good excuse!

I found that the following code for a procedure compiles using VAX PASCAL (and runs as expected)..

PROCEDURE format(number : INTEGER);
   VAR
      result : STRING(16);
   BEGIN
     :
     :
      writeln(result);
   END.

However if turn this into a function and try to return the result as a string it won't compile.

FUNCTION format(number : INTEGER) : STRING(16);
   VAR
      result : STRING(16);
   BEGIN
     :
     :
      format := result;
   END.

The error suggests that the error is at type definition for the function.

FUNCTION format(number : INTEGER) : STRING(16);
                                    1
PASCAL-E-TYPCNTDISCR, Type can not be discriminated in this context

I tried using VARYING and ARRAY types instead of STRING but they don't work either. Unfortunately I can't find an example of a function that returns a STRING in SYS$EXAMPLES or in the manuals I found of bitsavers.

Hopefully someone has a better memory than me.

Thanks


Solution

  • "Pascal's type system has been described as "too strong", because the size of an array or string is part of its type, ..." Strong and weak typing

    This gives a hint that the String(16) in the function return value is too vague for the compiler.

    Fix that by declaring a string type that suits the compiler:

    type 
      String16 = packed array[1..16] of char;
    

    Then you can use that distinct type in the function:

    FUNCTION format(number : INTEGER) : String16;
    VAR
      result : String16;
    BEGIN
      :
      :
      format := result;
    END.
    

    This is very much what was used in many early implementations of the pascal language (and Turbo Pascal), and is still valid. Modern compilers, like Delphi and FreePascal, has implemented a specialized dynamic array for strings, which covers a more convenient handling of the string type, not depending on declaring a strict size.