* * *

Author Topic: Val procedure  (Read 281 times)

bigmaneds

  • New member
  • *
  • Posts: 13
Val procedure
« on: May 05, 2017, 01:56:47 pm »
Hello! I have question, how can i Correctly execute val procedure, because all the time I have some kind of errors. I just started to learn functions and procedures and I don't quite understand variable exchange....
The errors I get, is : Identifier V, Code not found, could you please fix my line and explain please?

var vec:integer;

procedure Val(const S: string;  var V; var Code: Word);
begin
 end;     

begin
 writeln('Write only a number');
  readln(vec);
  val(vec,V,Code);   
end.         

yurkad

  • Jr. Member
  • **
  • Posts: 85
Re: Val procedure
« Reply #1 on: May 05, 2017, 02:19:23 pm »
After correct lines 12 and 13
following cod must work.
May be. I am beginner too...

Code: Pascal  [Select]
  1. var vec:integer;
  2. V:Integer;
  3. Code:Word;
  4.  
  5. procedure Val(const S: Integer;  var V:Integer; var Code: Word);
  6. begin
  7.  end;    
  8.  
  9. begin
  10.  writeln('Write only a number');
  11.   readln(vec);
  12.   V := ???
  13.   Code := ???
  14.   val(vec,V,Code);  
  15. end.  

May be

  V := 2;
  Code := 444;

In this case is working.
« Last Edit: May 05, 2017, 02:24:41 pm by yurkad »

Thaddy

  • Hero Member
  • *****
  • Posts: 3434
Re: Val procedure
« Reply #2 on: May 05, 2017, 02:30:42 pm »
Of course you will get errors. Val is part of  the system unit.... Sigh...

molly

  • Hero Member
  • *****
  • Posts: 1568
Re: Val procedure
« Reply #3 on: May 05, 2017, 02:31:16 pm »
I am assuming you want to use the val procedure that is part of unit system, see here

A variable parameter is a parameter that is (also) returned by a procedure or function. Also see here.

So, in contrary to your ordinary function that is only able to return a single return value, you can use var parameters to return a multiple amount of return values.

For that to work, you need to pass the correct variables (type) to the function or procedure.

In the case of procedure val:
- 1st parameter: const S: string;
- 2nd parameter: var V; (note that this variable is untyped)
- 3th parameter: var Code: Word

So, the first parameter is to be a string (string notation of a number).

The second parameter is an untyped variable but, the documentation writes about restrictions:
Quote
... variable V, which can be of type Longint, Real and Byte or any enumerated type.

And the last parameter should be a word.

Therefor, the correct call would be:
Code: Pascal  [Select]
  1. var
  2.   vec:string;  // note, that is variable is a string
  3.   StringAsInteger: Integer; // note, that this is an integer.
  4.   ErrorCode : Word;
  5.  
  6. begin
  7.  writeln('Write only a number');
  8.  // let user enter a string
  9.  // (not an integer, in opposite to your example that directly tries to read the input into an integer variable)
  10.  readln(vec);
  11.  // try to convert the string into a integer number, from var vec to var StringAsInteger
  12.  val(vec,StringAsInteger,ErrorCode);  
  13. end.        
  14.  

You can then check the returned ErrorCode to see if the 'conversion' from string to integer went ok.

edit: typo's + some additional comments
« Last Edit: May 05, 2017, 03:17:31 pm by molly »

Thaddy

  • Hero Member
  • *****
  • Posts: 3434
Re: Val procedure
« Reply #4 on: May 05, 2017, 02:33:36 pm »
Sorry. I was still working on a better explanation, but molly already did it and our posts crossed.

molly

  • Hero Member
  • *****
  • Posts: 1568
Re: Val procedure
« Reply #5 on: May 05, 2017, 03:18:08 pm »
Sorry, i crossed you with my first post Thaddy.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus