Page 1 of 1
Text as function result?
Posted: Mon Sep 13, 2021 10:09 am
by willemgrooters
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...)
Re: Text as function result?
Posted: Mon Sep 13, 2021 1:21 pm
by arne_v
You do not like to return a varying[MAXLEN] of char and waste some bytes of memory for storage?
Re: Text as function result?
Posted: Mon Sep 13, 2021 2:33 pm
by willemgrooters
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:
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:
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
)
Re: Text as function result?
Posted: Mon Sep 13, 2021 3:04 pm
by arne_v
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.
Re: Text as function result?
Posted: Wed Sep 15, 2021 8:52 pm
by jreagan
Everybody OK now? I don't come here often. Seems like you figured it out without me.
John