Lazarus

Programming => General => Topic started by: madref on December 10, 2017, 09:53:41 am

Title: [Solved] Nested function
Post by: madref 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.  
Title: Re: Nested function
Post by: FTurtle 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
Title: Re: Nested function
Post by: Thaddy 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.
Title: Re: Nested function
Post by: madref on December 10, 2017, 12:48:11 pm
I need to preserve the chipper because it's unique
Title: Re: Nested function
Post by: Thaddy on December 10, 2017, 01:02:45 pm
Then it is option 2?
Title: Re: [Solved] Nested function
Post by: madref 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
Title: Re: [Solved] Nested function
Post by: Thaddy 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.
Title: Re: [Solved] Nested function
Post by: CM630 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.
TinyPortal © 2005-2018