Text as function result?

Post Reply

Topic author
willemgrooters
Valued Contributor
Posts: 87
Joined: Fri Jul 12, 2019 1:59 pm
Reputation: 0
Location: Netherlands
Status: Offline
Contact:

Text as function result?

Post by willemgrooters » Mon Sep 13, 2021 10:09 am

I want to define a function (in Pascal) that takes a number as input and returns a string.
Like:

Code: Select all

function IntToStr (N:integer; z:boolean := false): <string definition>
which can be called as part of string definition, like:

Code: Select all

display ("status of call=" + intToStr(statuscode));
I would like the result to be string or varying (I'll take care to prevent under- and overflow), but I'm lost on how to define the result type...

(of course, I could

Code: Select all

procedure IntToStr (N:integer; var s: string; z:boolean := false)
but that would disable the wished construction...)

User avatar

arne_v
Master
Posts: 299
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: Text as function result?

Post by arne_v » Mon Sep 13, 2021 1:21 pm

You do not like to return a varying[MAXLEN] of char and waste some bytes of memory for storage?
Arne
arne@vajhoej.dk
VMS user since 1986


Topic author
willemgrooters
Valued Contributor
Posts: 87
Joined: Fri Jul 12, 2019 1:59 pm
Reputation: 0
Location: Netherlands
Status: Offline
Contact:

Re: Text as function result?

Post by willemgrooters » Mon Sep 13, 2021 2:33 pm

You do not like to return a varying[MAXLEN] of char and waste some bytes of memory for storage?
That's OK with me; but HOW do I define it in the function definition....

But this doesn't help.
Now I defined the function as:

Code: Select all

function IntToStr (I: integer; z:boolean:=false): varying [12] of char; 
because the result will - incluiding sign - never exceed 12 characters.
However, VMSIDE isn't happy with that:

Code: Select all

"missing ';' at '['"
at "["

Code: Select all

"mismatched input ';' expecting {';', END}" 
at function "end;" line

nor is the compiler:

Code: Select all

%PASCAL-E-SYNTYPID, Syntax: type identifier expected
at "varying".

Added

Code: Select all

type:  T_TxtNum = varying[12] of char;
and used that as type definition.

Code: Select all

function IntToStr (I: integer; z:boolean:=false): T_TxtNum; 

(Disgressing:

Why VMS-IDE tells me:

Code: Select all

"missing ';' at '['",
is (I guess) a bug in VMS-IDE (it won't disappear unless I close the whole session...)

)

but as it turned out, was indeed the solution (It's been many years I worked so regularly with VMS Pascal - now being spoiled using Delphi on Windows for quite some time... That might be the problem :D )
Last edited by willemgrooters on Mon Sep 13, 2021 3:14 pm, edited 1 time in total.

User avatar

arne_v
Master
Posts: 299
Joined: Fri Apr 17, 2020 7:31 pm
Reputation: 0
Location: Rhode Island, USA
Status: Offline
Contact:

Re: Text as function result?

Post by arne_v » Mon Sep 13, 2021 3:04 pm

Just create a type.

Example:

Code: Select all

program strfun(input, output);

type
    pstr = varying [32000] of char;

function f(n : integer) : pstr;

var
    res : pstr;
    i : integer;

begin
    res := '';
    for i := 1 to n do res := res + chr(48 + i mod 10);
    f := res;
end;

procedure test(n : integer);

var
    s : pstr;

begin
    s := f(n);
    writeln('|',s,'|');
    writeln(s.length);
    writeln(length(s.body));
end;

begin
    test(2);
    test(12);
end.
Added in 12 minutes 59 seconds:
Output:

Code: Select all

$ pas strfun
$ lin strfun
$ run strfun
|12|
         2
     32000
|123456789012|
        12
     32000
Added in 51 seconds:
Saw that you got it sorted out yourself.
Arne
arne@vajhoej.dk
VMS user since 1986


jreagan
VSI Expert
Master
Posts: 134
Joined: Tue Dec 01, 2020 8:40 am
Reputation: 0
Status: Offline

Re: Text as function result?

Post by jreagan » Wed Sep 15, 2021 8:52 pm

Everybody OK now? I don't come here often. Seems like you figured it out without me.

John

Post Reply