Author Topic: [Solved] The use of EXIT with no value in a function when the RESULT var is set?  (Read 4486 times)

jamie

  • Hero Member
  • *****
  • Posts: 7938
In a function where the RESULT or Function Return value has been set, and if EXIT is used without a value past to it, it seems the EXIT according to the DOCS I can find does not return the Functions result value, just simply returns undefined values?

 Is this true?

Jamie
« Last Edit: August 16, 2026, 05:51:35 pm by jamie »
The only true wisdom is knowing you know nothing

Thausand

  • Hero Member
  • *****
  • Posts: 613
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #1 on: August 16, 2026, 04:58:22 pm »
... just simply returns undefined values?

 Is this true?
No.
A docile goblin always follow HERMES.md

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #2 on: August 16, 2026, 05:12:58 pm »
That is:
- if result is not initialized: yes, undefined.
- if result is initialized: no, result is defined.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2.  
  3. function test1:integer;
  4. begin
  5.   exit;
  6. end;
  7.  
  8. function test2:integer;
  9. begin
  10.   Result := 10;
  11.   exit;
  12. end;
  13.  
  14. begin
  15.   writeln(test1);
  16.   writeln(test2);
  17.   readln;
  18. end.
Always initialize result if you are not sure.
The compiler also warns: Warning: (5033) Function result does not seem to be set
Which means the previous answer is plain wrong. Without initialization your assumption is correct: the answer is yes.
« Last Edit: August 16, 2026, 05:20:07 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

cdbc

  • Hero Member
  • *****
  • Posts: 2944
    • http://www.cdbc.dk
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #3 on: August 16, 2026, 05:17:27 pm »
Hey jamie
OY... What you been smoking mate, I want some  :D
Why does the compiler insist on us 'Initializing Result variable', if it doesn't matter...?
In {$mode objfpc} you can use both 'Result' & the function-name, for result returning, normally or via exit;
Regards Benny

Ps.: You see flying pigs too?!?  :D :D :D
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #4 on: August 16, 2026, 05:24:12 pm »
@Benny, run my code: his assumption is correct, but he did not examine the compiler warning with the answer staring him in his face.
"Warning: (5033) Function result does not seem to be set"

On my machine:
Code: Bash  [Select][+][-]
  1. 21584  #random stack
  2. 10 #initialized
So the correct answer is YES.
« Last Edit: August 16, 2026, 05:28:46 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

jamie

  • Hero Member
  • *****
  • Posts: 7938
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #5 on: August 16, 2026, 05:36:10 pm »
Hey jamie
OY... What you been smoking mate, I want some  :D
Why does the compiler insist on us 'Initializing Result variable', if it doesn't matter...?
In {$mode objfpc} you can use both 'Result' & the function-name, for result returning, normally or via exit;
Regards Benny

Ps.: You see flying pigs too?!?  :D :D :D

Please show me DOCS that indicates when using EXIT() , it will return the VALUE of RESULT or the Set value of the function like used in the older days.

I don't want to see AI statements since most of it is dream land or the AI server on drugs, I want to see this from DOCS supplied.

I understand if you don't set the RESULT variable it's going to be undefined anyways, but it does not state when using EXIT() the return value is the RESULT/Function result, That's my point.

Jamie

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #6 on: August 16, 2026, 05:38:35 pm »
Well, my code has nothing to do wih AI: if you do not initialize you are simply making programmer errors.
Any "programmer" that knows only one programming language is not a programmer

Thausand

  • Hero Member
  • *****
  • Posts: 613
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #7 on: August 16, 2026, 05:38:42 pm »
In a function where the RESULT or Function Return value has been set, and if EXIT is used without a value past to it,
...
Is this true?
Answer no.
A docile goblin always follow HERMES.md

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #8 on: August 16, 2026, 05:42:06 pm »
Yes in that case no, because you did initialize.
Aren't you reading? Or testing?
Parameterized Exit(-1) IS initialization of result, just as much as Result :='';
Unparameterized exits are undefined  and the compiler tells you so. <sigh>
Any "programmer" that knows only one programming language is not a programmer

jamie

  • Hero Member
  • *****
  • Posts: 7938
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #9 on: August 16, 2026, 05:50:54 pm »
Yes in that case no, because you did initialize.
Aren't you reading? Or testing?
Parameterized Exit(-1) IS initialization of result, just as much as Result :='';
Unparameterized exits are undefined  and the compiler tells you so. <sigh>

I did test it and got my answer long ago, the problem is, I can't find that explicit answer, it just leads me astray because it only talks about using EXIT in a procedure not a function and what would happen otherwise.

Thank you for your time, I wish the DOCS could be extended a little to explain that in details.

Jamie

The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 6587
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #10 on: August 16, 2026, 07:27:20 pm »
I understand if you don't set the RESULT variable it's going to be undefined anyways, but it does not state when using EXIT() the return value is the RESULT/Function result, That's my point.

Jamie
But the point you're missing is that a parameter-less "exit" will simply exit the function or procedure.  This means, in the case of a function, whatever result was set to (if set at all) is what is returned since the function was exited.

The documentation clearly states that whatever the value, if any, of "result" is what the function returns if the result's value isn't set using the exit statement's parameter value.

Succinctly, the documentation does state that the function's return value can be set using the "exit" parameter or setting the result variable then exiting the function.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6430
  • Compiler Developer
Re: The use of EXIT with no value in a function when the RESULT var is set?
« Reply #11 on: August 17, 2026, 10:17:22 pm »
Yes in that case no, because you did initialize.
Aren't you reading? Or testing?
Parameterized Exit(-1) IS initialization of result, just as much as Result :='';
Unparameterized exits are undefined  and the compiler tells you so. <sigh>

I did test it and got my answer long ago, the problem is, I can't find that explicit answer, it just leads me astray because it only talks about using EXIT in a procedure not a function and what would happen otherwise.

Exit with parameter is a much more recent addition (comparatively) and thus in both FPC and Delphi the main way of returning a value when exiting a routine is to set Result to the desired value and then call Exit. Exit with parameter is simply an optimization of that.

Thank you for your time, I wish the DOCS could be extended a little to explain that in details.

Well, feel free to open an issue report against the documentation to have that improved/clarified.

eny

  • Hero Member
  • *****
  • Posts: 1669
Actually the question is not that outrageous.
As mentioned above, the answer could indeed logically be derived from the docs.

But in the TP7.0 printed manuals in the days of yore, where the Exit  procedure (compiler intrinsic) is properly documented, Borland deemed it useful to make an explicit remark that "using Exit is the same as using GOTO to a label at the end of the procedure".
So it has no impact on any previously set function result.
« Last Edit: August 19, 2026, 06:29:29 pm by eny »
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

jamie

  • Hero Member
  • *****
  • Posts: 7938
Now you see, that description I would understand, it clearly states that any previous results is maintained in the case of EXIT wtih no parameter.

Thank you for that  ;)

Jamie
The only true wisdom is knowing you know nothing

eny

  • Hero Member
  • *****
  • Posts: 1669
Little nuance, the comment is just about the 'GOTO'.
The last sentence is my own comment.
I reformatted the text to make that clear.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

 

TinyPortal © 2005-2018