* * *

Author Topic: [Solved] Nested function  (Read 1335 times)

madref

  • Sr. Member
  • ****
  • Posts: 471
  • ..... A day not Laughed is a day not Lived !!
    • Nursing With Humour
[Solved] Nested function
« on: December 10, 2017, 09:53:41 am »
I am trying to have a nested function because i am calling this multiple times and i need the SAME cipher every time.


How to declare this function?
Because this doesn't work. I get empty strings all the time.


Code: Pascal  [Select]
  1.  
  2. procedure TForm_Options.BT_ApplyClick(Sender: TObject);
  3. var Cipher: TDCP_rc4;
  4.     Code: string;
  5.  
  6.  
  7.     function MyCode (MC: string): string;
  8.     begin
  9.       if MC = '' then
  10.         MyCode := ''
  11.       else
  12.         Cipher.EncryptString(MC);
  13.     end;     // MyCode
  14.  
  15.  
  16.  
  17.  
  18. begin
  19. .....
  20. .....
  21.  
  22.   Cipher:= TDCP_rc4.Create(Self);
  23.   Cipher.InitStr(cfgFile,TDCP_sha256);         // initialize the cipher with a hash of the passphrase
  24.   Code := MyCode(Edit_MailServer.Text);
  25. .....
  26. .....
  27. end;     // BT_ApplyClick
  28.  
« Last Edit: December 12, 2017, 12:25:04 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you win, no matter the outcome.

Lazarus 1.8.4 / FPC 3.0.4
Lazarus Trunk / FPC Trunk
Mac OS X High Sierra

FTurtle

  • Full Member
  • ***
  • Posts: 229
Re: Nested function
« Reply #1 on: December 10, 2017, 10:06:22 am »
Because this doesn't work. I get empty strings all the time.

You don't assign Result in function MyCode when occures else

Thaddy

  • Hero Member
  • *****
  • Posts: 6374
Re: Nested function
« Reply #2 on: December 10, 2017, 10:41:33 am »
Code: Pascal  [Select]
  1. //maybe:
  2. procedure TForm_Options.BT_ApplyClick(Sender: TObject);
  3. var Cipher: TDCP_rc4;
  4.     Code: string;
  5.  
  6.     function MyCode (var MC: string): string;
  7.     begin
  8.        Result := ''
  9.        
  10.        if MC <> '' then  
  11.        begin
  12.            Cipher.EncryptString(MC); // is in place, so make MC a var
  13.            Result := MC; // Return a valid result
  14.         end;
  15.     end;     // MyCode

Alternatively this may also be an option if you want to preserve MC:
Code: Pascal  [Select]
  1. procedure TForm_Options.BT_ApplyClick(Sender: TObject);
  2. var Cipher: TDCP_rc4;
  3.     Code: string;
  4.  
  5.     function MyCode (const MC: string): string;
  6.     begin
  7.        Result :=  MC; // MC is preserved as unencrypted here
  8.        
  9.        if Result <> '' then  
  10.            Cipher.EncryptString(Result); // is in place, result is encrypted
  11.  
  12.     end;     // MyCode

I don't know what your requirements are, but both examples are valid code.
The second one is faster, btw, and seems applicable given your code.
The first one can also be turned into a local procedure instead of a function.
« Last Edit: December 10, 2017, 10:58:23 am by Thaddy »
I am enjoining wine, not whine....

madref

  • Sr. Member
  • ****
  • Posts: 471
  • ..... A day not Laughed is a day not Lived !!
    • Nursing With Humour
Re: Nested function
« Reply #3 on: December 10, 2017, 12:48:11 pm »
I need to preserve the chipper because it's unique
You treat a disease, you win, you lose.
You treat a person and I guarantee you win, no matter the outcome.

Lazarus 1.8.4 / FPC 3.0.4
Lazarus Trunk / FPC Trunk
Mac OS X High Sierra

Thaddy

  • Hero Member
  • *****
  • Posts: 6374
Re: Nested function
« Reply #4 on: December 10, 2017, 01:02:45 pm »
Then it is option 2?
« Last Edit: December 10, 2017, 01:04:59 pm by Thaddy »
I am enjoining wine, not whine....

madref

  • Sr. Member
  • ****
  • Posts: 471
  • ..... A day not Laughed is a day not Lived !!
    • Nursing With Humour
Re: [Solved] Nested function
« Reply #5 on: December 12, 2017, 12:26:05 pm »
I changed it up a bit but this is the final code
Code: Pascal  [Select]
  1.     function MyEncryption (const MC: string): string;
  2.     begin
  3.       Result := '';
  4.       if MC <> '' then
  5.         Result := Cipher.EncryptString(MC);
  6.     end;     // MyEncryption
  7.  


This because else the result doesn't give the right outcome
You treat a disease, you win, you lose.
You treat a person and I guarantee you win, no matter the outcome.

Lazarus 1.8.4 / FPC 3.0.4
Lazarus Trunk / FPC Trunk
Mac OS X High Sierra

Thaddy

  • Hero Member
  • *****
  • Posts: 6374
Re: [Solved] Nested function
« Reply #6 on: December 12, 2017, 12:35:36 pm »
Uhmmmm: That generates worse code.... Although it is functionally the same. (examine output by compiling with -a)
My example (2) uses registers throughout and does not have to do the comparison using a memory location.
But your code works too, so if you are happy, I am happy... O:-)

Note in the case of encryption  it is better to hold code in registers and not overly access memory locations. That's why I wrote it that way. And it is faster.
It is only a minor point but it helps.
« Last Edit: December 12, 2017, 12:43:00 pm by Thaddy »
I am enjoining wine, not whine....

CM630

  • Hero Member
  • *****
  • Posts: 817
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [Solved] Nested function
« Reply #7 on: July 11, 2018, 07:28:41 am »
I just tried to use a nested function.
It occured that when I do „exit‟ or „exit(value)‟ from within the nexted function, this exit is applied to the nesting function.
Is it possible to exit nested function wihout exiting the nesting one?


Edit: It occurs problem is somewhere else. I have taken out nested function outside the nesting one and I still observe same behaviour.
« Last Edit: July 11, 2018, 08:08:21 am by CM630 »
Лазар 1,8,0;W7 64bit or XP 32bit;FPC3,0,4;rev 56594

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus