Recent

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

Alextp

  • Hero Member
  • *****
  • Posts: 776
    • 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

  • Full Member
  • ***
  • Posts: 225
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 1.8.4 FPC 3.0.4 x86_64-linux-gtk2 -- Ubuntu 18.04 XFCE

lucamar

  • Hero Member
  • *****
  • Posts: 887
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 1.8.4/FPC 3.0.4 on:
(K)Ubuntu 12..16, Windows XP SP3 (Home/Prof.) and various DOS incarnations.

MathMan

  • Full Member
  • ***
  • Posts: 150
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: 511
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.


ahydra

  • New member
  • *
  • Posts: 17
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: 511
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.