Home computickets Analog Procedure Val Pascal

Analog Procedure Val Pascal

Author

Date

Category

There is a val (string, byte / integer / longint, word) procedure, which returns a string representation of the number from parameter 1 to a numerical view, in a variable 2. 3 – it says about an error if it occurs .
In the 2 variable, BYTE, Integer or Longint are stored. It depends on what type of variable created and indicated in the parameter.

That’s the problem: I want to write such a procedure yourself, but I do not understand how to create a variable in the parameters of the procedure that becomes one of the three types, depending on which it put in the parameters when it is used.

For example:

Procedure Vall (ST: String; Var Byte / Integer / Longint; Var X: Word);
Begin.
  {...}
end;
var.
  S: String;
  X: Integer;
Begin.
  S: = '12345';
  Vall (S, X);
  Writeln (X);
end;

Question: How did the procedure make your second parameter by the Integer type? What do you need to write when creating a second parameter?


Answer 1, Authority 100%

It is impossible to create exactly such a procedure, because Val, Writeln, Str and some others belong to the so-called. “Completor Magic”. In fact, the compiler substitutes one of the functions with a typed argument in their place. This can be seen in the generated assembler code (Alt-Ctrl-c at the stop point)

val (s, intvar, c);
 007941B6 E8B578C7FF CALL @USTRLASG
 Val (s, dblvar, c);
 00794307 E88850C7FF CALL @VALEXT

Something similar can be done with Array of Const , only the argument will be in square brackets

Function Mystr (A: Array of Const): String;
Begin.
 Case A [0] .vtype of
  Vtinteger: Result: = inttostr (a [0] .vinteger);
  VTextended: Result: = Format ('% 5.3F', [A [0] .Vextended ^]);
 end;
end;
Memo1.Lines.add (MYSTR ([I]));
Memo1.Lines.add (MYSTR ([D]));

or Variant


Answer 2, Authority 100%

Function Overload / Procedures

Function Whattype (Const n: Integer): String;
Begin.
  Whattype: = 'Integer';
end;
FUNCTION WHATTYPE (Const N: Real): String;
Begin.
  Whattlepe: = 'Real';
end;
FUNCTION WHATTYPE (Const N: Byte): String;
Begin.
  Whattype: = 'Byte';
end;
var.
  B: BYTE;
  R: Real;
  I: integer;
Begin.
  Writeln (Whattlepe (i));
  Writeln (Whattype (R));
  Writeln (Whattype (B));
end.

and advise, since write in your own way, then write on procedures, and features, and more convenient to use and can then be used

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions