t89_varstr.pas 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. program t89_varstr;
  2. { t89 -- VAR string parameters. CALC.INC's GetFileName is
  3. procedure GetFileName(var Line : AnyString; FileType : String3);
  4. and the callee must operate on the CALLER's string, not on a copy. This
  5. exercises the address slot for the whole variable, a constant-index element
  6. (x[1]), a variable-index element (x[i]), Length, assignment of a literal and
  7. of a concatenation, and a function whose formal is VAR string.
  8. Hand-derived output:
  9. hello setit copies the literal through the caller's address
  10. 5 Length reads the length byte through that address
  11. Hello! upd writes x[1] and x[0], then appends through the address
  12. H the same element read back
  13. }
  14. var
  15. S : string[10];
  16. I : integer;
  17. procedure setit (var X : string[10]);
  18. begin
  19. X := 'hello'
  20. end;
  21. procedure upd (var X : string[10]);
  22. var
  23. I : integer;
  24. begin
  25. X[1] := 'H';
  26. X[0] := chr (5);
  27. for I := 1 to Length (X) do
  28. X[I] := X[I];
  29. X := X + '!'
  30. end;
  31. function size (var X : string[10]) : integer;
  32. begin
  33. size := Length (X)
  34. end;
  35. begin
  36. setit (S);
  37. writeln (S);
  38. writeln (size (S));
  39. upd (S);
  40. writeln (S);
  41. I := 1;
  42. writeln (S[I])
  43. end.