Recent

Author Topic: [SOLVED] Is there a best method for returning a function value?  (Read 22308 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6396
  • Compiler Developer
Re: Is there a best method for returning a function value?
« Reply #75 on: April 18, 2020, 12:07:11 pm »
Yes, that's essentially how it works. No need to make this more complex than necessary.
Just curiousity.... wouldn't allowing the "&" as a valid character in an identifier (and possibly requiring that if used, it must be the first character) have accomplished the same thing ?... am I missing some case where that would not have worked ?

Not really, because the purpose of & is to convert keywords (like type or end) to identifiers. These are handled differently by the scanner than identifiers. Despite this the & prefix needs to work for non-keyword identifiers as well. The scanner is already on the lookout for &, because that's the prefix for octal numbers in FPC; also in mode MacPas & and | are used for shorthand boolean evaluation expressions. Thus it was easier to add it to the scanner this way.

The code as it is is rather simple (from scanner.pas, line 4810):

Code: Pascal  [Select][+][-]
  1.              '&' :
  2.                begin
  3.                  if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4.                   begin
  5.                     readnumber;
  6.                     if length(pattern)=1 then
  7.                       begin
  8.                         { does really an identifier follow? }
  9.                         if not (c in ['_','A'..'Z','a'..'z']) then
  10.                           message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  11.                         readstring;
  12.                         token:=_ID;
  13.                         idtoken:=_ID;
  14.                       end
  15.                     else
  16.                       token:=_INTCONST;
  17.                     goto exit_label;
  18.                   end
  19.                  else if m_mac in current_settings.modeswitches then
  20.                   begin
  21.                     readchar;
  22.                     token:=_AMPERSAND;
  23.                     goto exit_label;
  24.                   end
  25.                  else
  26.                   Illegal_Char(c);
  27.                end;
  28.  

440bx

  • Hero Member
  • *****
  • Posts: 6532
Re: Is there a best method for returning a function value?
« Reply #76 on: April 18, 2020, 01:21:39 pm »
The scanner is already on the lookout for &, because that's the prefix for octal numbers in FPC; also in mode MacPas & and | are used for shorthand boolean evaluation expressions. Thus it was easier to add it to the scanner this way.
I see.  Thank you for the additional information.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

GypsyPrince

  • Guest
Re: Is there a best method for returning a function value?
« Reply #77 on: April 19, 2020, 10:55:57 pm »
@zamronypj

Quote
I wish that "result" variable can be mapped to another more meaningful name.

I am unsure if this is what you meant.  But this is how I "remap" the result keyword...

Code: Pascal  [Select][+][-]
  1. {$macro on}{$Define Return:=result}
  2.  
  3. Procedure MyProc(Param1: Type1; Param2: Type2): Int64;
  4.   var intRslt : Int64;
  5.  
  6. Begin
  7.     intRslt := <somevalue>
  8.  
  9.     Return := intRslt;
  10. End;
  11.  

Because of my C background (and .NET more recently), 'Return' just seems far more intuitive to me.

Likewise, it screws with my head for an If/Else block to not use an EndIf terminator.  So, I use this, instead...

Code: Pascal  [Select][+][-]
  1. {$macro on}{$Define Return:=result}{$Define End_If:=//}
  2.  
  3. Procedure MyProc(Param1: Type1; Param2: Type2): Int64;
  4.   var intRslt : Int64;
  5.  
  6. Begin
  7.     intRslt := somevalue
  8.  
  9.     If intRslt <> someothervalue Then
  10.         intRslt := someothervalue;
  11.     End_If; //False terminator
  12.  
  13.     Return := intRslt;
  14. End;

The reason I do it is because

Code: Pascal  [Select][+][-]
  1. If intRslt <> someothervalue Then
  2.     intRslt := someothervalue;

screws with my brain when I see it with no terminator.  What would really make my day is if fp would stop using the curly brackets "{ }" to enclose directives and comments so they could instead be remapped via a macro to take the place of the 'begin' and 'end' keywords.  I could do this...

Code: Pascal  [Select][+][-]
  1. [$macro on][$Define Return:=result][$Define {:=begin][$Define }:=end][$Define End_If:=//]
  2.  
  3. Procedure MyProc(Param1: Type1; Param2: Type2): Int64;
  4.   var intRslt : Int64;
  5. {
  6.     intRslt := somevalue
  7.  
  8.     If intRslt <> someothervalue Then
  9.         intRslt := someothervalue;
  10.     End_If; //False terminator
  11.  
  12.     Return := intRslt;
  13. };


Then, I could perform my Snoopy happy dance!  But, alas... this will never come to be (and I won't ask for it).  And unfortunately, as well, my beloved C will never be available in a WYSIWYG/RAD IDE.
« Last Edit: April 20, 2020, 12:14:24 am by GypsyPrince »

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Is there a best method for returning a function value?
« Reply #78 on: April 20, 2020, 01:50:20 am »
The reason I do it is because

Code: Pascal  [Select][+][-]
  1. If intRslt <> someothervalue Then
  2.     intRslt := someothervalue;

screws with my brain when I see it with no terminator.

Code: Pascal  [Select][+][-]
  1. If intRslt <> someothervalue Then
  2. begin
  3.   intRslt := someothervalue;
  4. end;

If that will ease your mind.
It's only logical.

zamronypj

  • Full Member
  • ***
  • Posts: 140
    • Fano Framework, Free Pascal web application framework
Re: Is there a best method for returning a function value?
« Reply #79 on: April 20, 2020, 04:27:52 am »
@zamronypj

Quote
I wish that "result" variable can be mapped to another more meaningful name.

I am unsure if this is what you meant.  But this is how I "remap" the result keyword...

Code: Pascal  [Select][+][-]
  1. {$macro on}{$Define Return:=result}
  2.  
  3. Procedure MyProc(Param1: Type1; Param2: Type2): Int64;
  4.   var intRslt : Int64;
  5.  
  6. Begin
  7.     intRslt := <somevalue>
  8.  
  9.     Return := intRslt;
  10. End;
  11.  


"result" is special variable inside a function. Your solution requires temporary local variable to be declared inside function. What i mean is a way to rename "result" variable to something else and compiler treats it as result variable of function without need to declare another local variable.

Macro helps but i think it is quite cumbersome to use if I have many functions that I want to map its result variable
« Last Edit: April 20, 2020, 04:37:12 am by zamronypj »
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

Otto

  • Full Member
  • ***
  • Posts: 226
Re: Is there a best method for returning a function value?
« Reply #80 on: April 20, 2020, 10:01:10 am »
 @GypsyPrince
Hello.

The reason I do it is because

Code: Pascal  [Select][+][-]
  1. If intRslt <> someothervalue Then
  2.     intRslt := someothervalue;

screws with my brain when I see it with no terminator.

Code: Pascal  [Select][+][-]
  1. If intRslt <> someothervalue Then
  2. begin
  3.   intRslt := someothervalue;
  4. end;

If that will ease your mind.
Maybe the "Jedi Code Formatter" could help you.
“Source>>Jedi Code Formatter>>Format Setting>>Clarify>>Transform>>Add begin and end to single statements”

 
I used it to solve a very different problem, which I described in this thread, but using different configurations might be useful to you.
There are also other similar tools that could be used as an alternative.
Kind regards.

 

TinyPortal © 2005-2018