Recent

Author Topic: [Solved] FPC dont give a warning in this case  (Read 2834 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2386
    • UVviewsoft
[Solved] FPC dont give a warning in this case
« on: January 31, 2019, 12:49:48 pm »
Im not sure, but seems, FPC 3.0.4 dont give a warning:

NN:= 2;
SomeProc(NN):

procedure SomeProc(out Val: integer);

Because of 'out' parameter, assignment NN:= 2 makes no sense. right?
« Last Edit: January 31, 2019, 03:40:03 pm by Alextp »

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: FPC dont give a warning in this case?
« Reply #1 on: January 31, 2019, 01:27:52 pm »
Im not sure, but seems, FPC 3.0.4 dont give a warning:

NN:= 2;
SomeProc(NN):

procedure SomeProc(out Val: integer);

Because of 'out' parameter, assignment NN:= 2 makes no sense. right?

A parameter of 'out' type, makes no guaranty that variable actually changed inside SomeProc body.
So initial assignment could make a sense.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: FPC dont give a warning in this case?
« Reply #2 on: January 31, 2019, 03:06:24 pm »
What warning were you expecting? "Code makes no sense"? :)

While quite clever, in their way, compilers are not (yet?) intelligent enough to know whether your code makes sense or not; they only note sintax errors, type conversion gotchas, and a long etc.

Your code may make not sense to you but the compiler only knows that it's perfectly aceptable code: there is a variable assignment a call to a procedure, etc. The procedure parameter is declared as out, so it knows it doesn't need to copy a value but pass a simple "write-only" reference and that's all.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: [Solved] FPC dont give a warning in this case
« Reply #3 on: January 31, 2019, 03:58:05 pm »
Im not sure, but seems, FPC 3.0.4 dont give a warning:

NN:= 2;
SomeProc(NN):

procedure SomeProc(out Val: integer);

Because of 'out' parameter, assignment NN:= 2 makes no sense. right?

I think it does make sense as a two way interface for a variable - I am using this in some functions.

The compiler does not give a warning but it at least hints iirc for the line where you first access "Val"
you get "Hint: variable 'Val' does not seem to be initialized" (or somesuch - not at my PC atm so top of my head).

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: [Solved] FPC dont give a warning in this case
« Reply #4 on: January 31, 2019, 04:04:24 pm »
Im not sure, but seems, FPC 3.0.4 dont give a warning:

NN:= 2;
SomeProc(NN):

procedure SomeProc(out Val: integer);

Because of 'out' parameter, assignment NN:= 2 makes no sense. right?
The assignment before calling SomeProc makes sense.  SomeProc could use the parameter before assigning it a new value which could very well depend on its initial value.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ahydra

  • New Member
  • *
  • Posts: 19
Re: [Solved] FPC dont give a warning in this case
« Reply #5 on: February 11, 2019, 05:45:13 am »
The assignment before calling SomeProc makes sense.  SomeProc could use the parameter before assigning it a new value which could very well depend on its initial value.

But in that case, one should use a var parameter, not an out parameter. (var = "in/out", similar to ref in C#)

440bx

  • Hero Member
  • *****
  • Posts: 3945
Re: [Solved] FPC dont give a warning in this case
« Reply #6 on: February 11, 2019, 06:10:51 am »
The assignment before calling SomeProc makes sense.  SomeProc could use the parameter before assigning it a new value which could very well depend on its initial value.

But in that case, one should use a var parameter, not an out parameter. (var = "in/out", similar to ref in C#)
No.  The purpose of "out" is to inform the compiler that the parameter will be written to in the function/procedure.  It is unfortunately a common misconception to conclude that because the parameter is written to in the procedure, it has not been initialized to some value before calling the procedure. 

Out neither says nor implies anything about the initial value of the parameter _before_ it is passed as a parameter.  It may or may not have been initialized and, that is true too for a var, therefore equating var to in (which implies an initial value) and/or out is incorrect.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018