Recent

Author Topic: Error with function and out and var parameters  (Read 5295 times)

runs

  • New Member
  • *
  • Posts: 26
Error with function and out and var parameters
« on: April 29, 2016, 03:44:17 pm »
I want a function returning more than one variable. When I insert a var or out parameter in a working function Lazarus aborts when compile the program.
I simply add to the function:
...;out my_var:string='')
Also Lazarus forces to enter a default value.
What I doing wrong?
Projects: LibreStaff

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Error with function and out and var parameters
« Reply #1 on: April 29, 2016, 03:57:20 pm »
I don't know if a return parameter can have a default value.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Error with function and out and var parameters
« Reply #2 on: April 29, 2016, 04:14:39 pm »
What I doing wrong?
nothing, or it must be you didn't read the error message:
Quote
Error: Default values can only be specified for value, const and constref parameters
at least, that is what my compiler told me.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Error with function and out and var parameters
« Reply #3 on: April 29, 2016, 06:09:09 pm »
Please give us the complete function definition along with the {$mode } setting.
Some modes don't know out, so they think out is the name of a variable...

Bart

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Error with function and out and var parameters
« Reply #4 on: April 29, 2016, 07:13:09 pm »
See also here:
Quote
Needless to say, default values are not supported for out parameters.
Code: [Select]
// procedure test(one: string; var two: string = '');
// default = Fatal: Syntax error, ")" expected but "=" found
// objfpc = Error: Default values can only be specified for value, const and constref parameters
// delphi = Error: Default values can only be specified for value, const and constref parameters
//
// procedure test(one: string; out two: string = '');
// default: Fatal: Syntax error, ":" expected but "identifier TWO" found
// delphi: Error: Default values can only be specified for value, const and constref parameters
// objfpc: Error: Default values can only be specified for value, const and constref parameters

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Error with function and out and var parameters
« Reply #5 on: April 29, 2016, 08:59:48 pm »
Also Lazarus forces to enter a default value.
What I doing wrong?

You need to rearrange the order of your parameters.
Any default parameters must be contiguous and at the end of the parameter list.
out parameters cannot have default values.*
So collect any out parameters at the beginning of your parameter list (or at least in front of the ones with default values), and place any parameters with default values at the end.

*That is, out parameters cannot have any default value defined in the parameter list in the procedure/function signature.
It is good practice to give all out parameters some appropriate default value at the beginning of the procedure implementation.
« Last Edit: April 29, 2016, 09:04:17 pm by howardpc »

runs

  • New Member
  • *
  • Posts: 26
Re: Error with function and out and var parameters
« Reply #6 on: April 29, 2016, 09:02:24 pm »
The default value for out is required by the compiler (the compiler raises an error if not).

The code is the following, a custom in put box form, the function is "CustomInputBox".

Code: Pascal  [Select][+][-]
  1. unit FormInputBox;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   FraAcceptCancel;
  10.  
  11. type
  12.  
  13.   { TFrmInputBox }
  14.  
  15.   TFrmInputBox = class(TForm)
  16.     EdiInput: TEdit;
  17.     FraAcceptCancel1: TFraAcceptCancel;
  18.     LblPrompt: TLabel;
  19.     LblCaption: TLabel;
  20.     procedure BtnAcceptClick(Sender: TObject);
  21.     procedure BtnCancelClick(Sender: TObject);
  22.     procedure EdiInputKeyPress(Sender: TObject; var Key: char);
  23.   private
  24.     { private declarations }
  25.   public
  26.     { public declarations }
  27.     function CustomInputBox(IptCaption:String=''; IptPrompt:String=''; DefaultValue: String=''; MaxLength:Integer=255; [b][color=red]out OutValue:String=''[/color][/b]): String;
  28.   end;
  29.  
  30. var
  31.   FrmInputBox: TFrmInputBox;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. procedure TFrmInputBox.EdiInputKeyPress(Sender: TObject; var Key: char);
  38. begin
  39.   if (Key= #13) then //if ENTER key pressed, close modal
  40.     begin
  41.     FraAcceptCancel1.BtnAccept.Click;
  42.     end
  43.   else if (Key= #27) then //if ESC key pressed, cancel modal
  44.     begin
  45.      FraAcceptCancel1.BtnCancel.Click;
  46.     end;
  47. end;
  48.  
  49. procedure TFrmInputBox.BtnAcceptClick(Sender: TObject);
  50. begin
  51.   ModalResult:= mrOK;
  52. end;
  53.  
  54. procedure TFrmInputBox.BtnCancelClick(Sender: TObject);
  55. begin
  56.   ModalResult:= mrCancel;
  57. end;
  58.  
  59. function TFrmInputBox.CustomInputBox(IptCaption:String=''; IptPrompt:String='';
  60.   DefaultValue: String=''; MaxLength:Integer=255; out OutValue:String=''): String;
  61. begin
  62.   with TFrmInputBox.Create(Application) do
  63.   try
  64.     LblCaption.Caption:= IptCaption;
  65.     LblPrompt.Caption:= IptPrompt;
  66.     EdiInput.Text:= DefaultValue;
  67.     EdiInput.MaxLength:= MaxLength;
  68.     ShowModal;
  69.     Result:= EdiInput.Text;
  70.   finally
  71.     FrmInputBox.Free;
  72.     FrmInputBox:= nil;
  73.   end;
  74. end;
  75.  
  76. end.
                                                                                                             
Projects: LibreStaff

runs

  • New Member
  • *
  • Posts: 26
Re: Error with function and out and var parameters
« Reply #7 on: April 29, 2016, 09:06:30 pm »
Yes, I put the out in the first place without default value and now it compiles.
If the order matters, I think it is a strange behaviour.
 ::)

Also Lazarus forces to enter a default value.
What I doing wrong?

You need to rearrange the order of your parameters.
Any default parameters must be contiguous and at the end of the parameter list.
out parameters cannot have default values.*
So collect any out parameters at the beginning of your parameter list (or at least in front of the ones with default values), and place any parameters with default values at the end.

*That is, out parameters cannot have any default value defined in the parameter list in the procedure/function signature.
It is good practice to give all out parameters some appropriate default value at the beginning of the procedure implementation.
« Last Edit: April 29, 2016, 09:08:12 pm by runs »
Projects: LibreStaff

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Error with function and out and var parameters
« Reply #8 on: April 29, 2016, 09:12:16 pm »
Such restrictions are one of the many reasons why Pascal compilers parse and compile code like lightning compared to most C++ compilers.

linuxfan

  • Jr. Member
  • **
  • Posts: 57
Re: Error with function and out and var parameters
« Reply #9 on: May 02, 2016, 09:48:28 am »
C++ poses the same restriction on default values for parameters. And it is logical, otherwise calling a function with default parameters could get ambiguous. But it is true that pascal code can be compiled much more fast than C/C++.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Error with function and out and var parameters
« Reply #10 on: May 02, 2016, 09:58:26 am »
...;out my_var:string='')
Also Lazarus forces to enter a default value.
What I doing wrong?

Well an out parameter can not have a default value by design.
See http://freepascal.org/docs-html/current/ref/refsu67.html#x178-20000014.4.3

Note a string is a managed type, so initializing it would lead to a memory leak.
To prevent that, you can never initialize an out parameter outside of the function body.
« Last Edit: May 02, 2016, 10:00:18 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018