Recent

Author Topic: Result variable multiple assignments in function  (Read 7535 times)

FRex

  • New Member
  • *
  • Posts: 20
Result variable multiple assignments in function
« on: December 11, 2017, 09:11:59 am »
Is it okay to write to a Result variable in function multiple times? Will only the last write count as the return value from this function?
App to keep track of notes in a single plain text file tagged by hashtags: https://github.com/FRex/botes

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Result variable multiple assignments in function
« Reply #1 on: December 11, 2017, 09:16:18 am »
Try this and see what happens:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. function Test: Integer;
  6. begin
  7.   Result := 1;
  8.   Result := 2;
  9.   Result := 3;
  10. end;
  11.  
  12. begin
  13.   WriteLn(Test());
  14.  
  15.   Readln;
  16. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14387
  • Sensorship about opinions does not belong here.
Re: Result variable multiple assignments in function
« Reply #2 on: December 11, 2017, 09:50:09 am »
IOW yes, the last one is the final result. What that means, however, is that you can use result internally as a local variable to perform calculations inside a function.
Code: Pascal  [Select][+][-]
  1. function UseResult(const s:string):string;
  2. begin
  3.   Result := s;  // use result as an internal local variable
  4.   Result := Upcase(s);  //final result
  5. end;
« Last Edit: December 11, 2017, 09:51:47 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

FRex

  • New Member
  • *
  • Posts: 20
Re: Result variable multiple assignments in function
« Reply #3 on: December 11, 2017, 12:04:06 pm »
I know it works experimentally but my background is in C and C++ so I wanted to ask in case it's platform or options dependent feature like many in C and C++ often are.
App to keep track of notes in a single plain text file tagged by hashtags: https://github.com/FRex/botes

dbannon

  • Hero Member
  • *****
  • Posts: 2802
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Result variable multiple assignments in function
« Reply #4 on: December 11, 2017, 12:19:18 pm »
FRex, its widely done in lots of code that you will come across using Lazarus. I've seen it done in LCL and CCR components for example.

I've seen code where Result is passed to other methods to do what they will with it.

Its good, save a local variable in lots of cases.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

FRex

  • New Member
  • *
  • Posts: 20
Re: Result variable multiple assignments in function
« Reply #5 on: December 11, 2017, 12:25:27 pm »
I'm slowly relearning Pascal after 5 years of not using it at all and the last one I used was some old Delphi. I'm not reading LCL source code to learn.
App to keep track of notes in a single plain text file tagged by hashtags: https://github.com/FRex/botes

Thaddy

  • Hero Member
  • *****
  • Posts: 14387
  • Sensorship about opinions does not belong here.
Re: Result variable multiple assignments in function
« Reply #6 on: December 11, 2017, 01:17:17 pm »
I know it works experimentally but my background is in C and C++ so I wanted to ask in case it's platform or options dependent feature like many in C and C++ often are.
My example is not experimental at all but main stream in the FPC/Delphi Pascal world. It indeed saves a local variable and an assignment.
Some - like me - prefer it over a superfluous local. It is also fully cross-platform.
As far as the language dialect goes:
It needs {$mode objfpc} or {$mode Delpi} of {$mode Delphiunicode} It can be enabled in other supported dialects by {$modeswitch result} (Which is implied in the first three)
« Last Edit: December 11, 2017, 01:28:05 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

FRex

  • New Member
  • *
  • Posts: 20
Re: Result variable multiple assignments in function
« Reply #7 on: December 11, 2017, 02:08:45 pm »
By experimental I meant trying out a piece of code to see what happens not if something is stable or not.
App to keep track of notes in a single plain text file tagged by hashtags: https://github.com/FRex/botes

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: Result variable multiple assignments in function
« Reply #8 on: December 11, 2017, 02:33:41 pm »
No problem with that, I often use the result variable to do stuff in functions - like this:

Code: Pascal  [Select][+][-]
  1. function FindString(const S: string; const List: TStrings): Integer;
  2. begin
  3.   for Result:= 0 to List.Count - 1 do if List[Result] = S then Exit;
  4.   Result:= -1;
  5. end;
  6.  

wildfire

  • Full Member
  • ***
  • Posts: 109
Re: Result variable multiple assignments in function
« Reply #9 on: December 11, 2017, 02:50:11 pm »
By experimental I meant trying out a piece of code...

In which case it's a simple experiment, why didn't you just try it?

Use a Black Box
« Last Edit: December 11, 2017, 02:57:22 pm by wildfire »
A halo is a mere circle, when does it end?

Thaddy

  • Hero Member
  • *****
  • Posts: 14387
  • Sensorship about opinions does not belong here.
Re: Result variable multiple assignments in function
« Reply #10 on: December 11, 2017, 03:15:46 pm »
When you are experimenting, also have a look at exit() which is an alternative way of returning from a function.
Slighty modified your own example for this:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. function Test: Integer;
  6. begin
  7.   Exit(1);
  8.   Result := 2;
  9.   Result := 3;
  10. end;
  11.  
  12. begin
  13.   WriteLn(Test());
  14.   Readln;
  15. end.

If you come from C(++) this may be more as you would expect. As you see Both can be combined..
« Last Edit: December 11, 2017, 03:27:45 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Result variable multiple assignments in function
« Reply #11 on: December 11, 2017, 05:02:34 pm »
This only works in an option range (I think) is set. Otherwise it results an error.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 14387
  • Sensorship about opinions does not belong here.
Re: Result variable multiple assignments in function
« Reply #12 on: December 11, 2017, 05:21:02 pm »
This only works in an option range (I think) is set. Otherwise it results an error.
No it works as expected. The result values with not be executed, the function exits immediately with the value 1..
I gave this example to experiment....
First compile and run before you shout error.... That's not true. >:D
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14387
  • Sensorship about opinions does not belong here.
Re: Result variable multiple assignments in function
« Reply #13 on: December 11, 2017, 05:31:49 pm »
@Mangakissa here' a more useful example that also always works:
Code: Pascal  [Select][+][-]
  1. function Test(const value:integer): Integer;
  2. begin
  3.   if value < 0 then exit(0);
  4.   if value > 99 then exit(99);
  5.   Result := value;
  6. end;

No else needed. Compare to:
Code: Pascal  [Select][+][-]
  1. function Test(const value:integer): Integer;
  2. begin
  3.   if value < 0 then result := 0 else
  4.     if value > 99 then result :=99 else
  5.       Result := value;
  6. end;

Which is equivalent.
« Last Edit: December 11, 2017, 05:35:20 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Result variable multiple assignments in function
« Reply #14 on: December 11, 2017, 05:36:26 pm »
Perhaps OP is more confident after reading the user manual on functions/result.

The only other thing that might perhaps be worth mentioning to a 'newbie' is that you can exit a function without providing a parameter to exit. That might work confusing if you aren't aware. e.g. you can mix result := x and make a call to exit; that way.

 

TinyPortal © 2005-2018