Recent

Author Topic: [solved] ignoring a function result  (Read 1715 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 2747
  • Compiler Developer
Re: ignoring a function result
« Reply #30 on: February 21, 2021, 04:30:45 pm »
[…] Does that /have/ to be in a separate unit? […]
External does not mean it’s in a separate unit. Both declarations are in the same file. And actually they have to be in the same unit, otherwise it doesn’t work transparently. If they were in separate units, you would need to specify the FQI for either routine, because the order units are listed in the uses clause matters.

You're wrong. By declaring it as public name 'my_function' you're declaring a non mangled assembly symbol that is useable across units. In fact if you use public name 'my_function' from multiple units you'll in fact get a "duplicate symbol" error. Thus you can import in from any unit as external name 'my_function' and even from e.g. statically linked C code.

A general note for this "functionality": don't use this with managed types or records as the former need to be finalized by the compiler to avoid memory leaks and the later will likely be passed as a hidden parameter thus leading to different ABIs for the procedure and the function.

To answer the question of the OP: yes, in general it is safe to ignore results, at least if you're dealing with simple or managed types (this also includes records). What you need to pay attention of are allocations inside the functions (e.g. if the function does a TSomeClass.Create or GetMem), cause then you need to free it yourself to avoid memory leaks.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2061
Re: ignoring a function result
« Reply #31 on: February 21, 2021, 07:17:11 pm »
What you need to pay attention of are allocations inside the functions (e.g. if the function does a TSomeClass.Create or GetMem), cause then you need to free it yourself to avoid memory leaks.

Presumably you're referring to result := TSomeClass.Create etc. here. Don;t know about anybody else but I always explicitly note in a function's comment if it returns something that needs to be freed by the caller... if a programmer ignores that then it's his problem (and job).

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2747
  • Compiler Developer
Re: ignoring a function result
« Reply #32 on: February 22, 2021, 09:42:22 am »
What you need to pay attention of are allocations inside the functions (e.g. if the function does a TSomeClass.Create or GetMem), cause then you need to free it yourself to avoid memory leaks.

Presumably you're referring to result := TSomeClass.Create etc. here. Don;t know about anybody else but I always explicitly note in a function's comment if it returns something that needs to be freed by the caller... if a programmer ignores that then it's his problem (and job).

That is indeed the best one can do in that case.

Zvoni

  • Sr. Member
  • ****
  • Posts: 461
Re: ignoring a function result
« Reply #33 on: February 22, 2021, 10:09:16 am »
Of course, doing the "devil's advocate" thing >:D one might contend that TStrings.Add doesn't need to return the index, since it's already known (TStrings.Count), so making it a function is pointless.

But then, as a function returning the index if you need it allows you to avoid further accesses to get it through Count.

Errr???
IIRC, what's the Index of an added Item, if TStrings/TStringList is sorted?
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Zoran

  • Hero Member
  • *****
  • Posts: 1652
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: ignoring a function result
« Reply #34 on: February 22, 2021, 12:10:13 pm »
Of course, doing the "devil's advocate" thing >:D one might contend that TStrings.Add doesn't need to return the index, since it's already known (TStrings.Count), so making it a function is pointless.

But then, as a function returning the index if you need it allows you to avoid further accesses to get it through Count.

Errr???
IIRC, what's the Index of an added Item, if TStrings/TStringList is sorted?

What do you find confusing there?

In sorted list, the newly added item will get its position in sort, and the function will return the index; in the next line of code, you can access this element by index.

The returned index is even more useful in sorted list, as it is not simply Count - 1.

Sieben

  • Full Member
  • ***
  • Posts: 206
Re: ignoring a function result
« Reply #35 on: February 22, 2021, 12:15:16 pm »
'Thread of Confusion' - I think this is exactly what Zvoni wanted to point at...  ;)

BTW - Zoran, did you see this...?
« Last Edit: February 22, 2021, 01:20:34 pm by Sieben »
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zvoni

  • Sr. Member
  • ****
  • Posts: 461
Re: ignoring a function result
« Reply #36 on: February 22, 2021, 12:52:59 pm »
Of course, doing the "devil's advocate" thing >:D one might contend that TStrings.Add doesn't need to return the index, since it's already known (TStrings.Count), so making it a function is pointless.

But then, as a function returning the index if you need it allows you to avoid further accesses to get it through Count.

Errr???
IIRC, what's the Index of an added Item, if TStrings/TStringList is sorted?

What do you find confusing there?

In sorted list, the newly added item will get its position in sort, and the function will return the index; in the next line of code, you can access this element by index.

The returned index is even more useful in sorted list, as it is not simply Count - 1.

Zoran, exactly my point.
I was pointing to the statement, that Add doesn't need to return the index, since you have it with Count, which doesn't work with sorted TStrings/TStringList
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

lucamar

  • Hero Member
  • *****
  • Posts: 3591
Re: ignoring a function result
« Reply #37 on: February 22, 2021, 02:06:52 pm »
I was pointing to the statement, that Add doesn't need to return the index, since you have it with Count, which doesn't work with sorted TStrings/TStringList

You're right, of course, and it's probably the reason (one of them) why Add returns the index.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASBzone

  • Hero Member
  • *****
  • Posts: 534
  • Automation leads to relaxation...
    • Free Console Utilities for Windows from BrainWaveCC
Re: ignoring a function result
« Reply #38 on: February 22, 2021, 05:02:04 pm »
...

sometimes I want to ignore the function's return value:
...
Is it ok to not 'catch' the result!?

cheers
S.

The short answer is, "yes, but there are a few caveats".

The longer answer is covered adequately in the thread.

Indiscriminate use of any such technique could lead to potential problems, but using this methodology sparingly is unlikely to cause significant problems. 

NOTE: @PascalDragon did point out a significant issue to consider, however.
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.11 r64280 / FPC v3.2.1-r48417 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042)
Other Systems: Windows 10 Pro x64, Version 2009 (Build 19042) or greater

speter

  • Full Member
  • ***
  • Posts: 154
Re: [solved] ignoring a function result
« Reply #39 on: February 23, 2021, 12:19:16 am »
Thanks to everyone to a most interesting discussion.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Zoran

  • Hero Member
  • *****
  • Posts: 1652
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: ignoring a function result
« Reply #40 on: February 24, 2021, 10:17:11 am »
Zoran, exactly my point.
I was pointing to the statement, that Add doesn't need to return the index, since you have it with Count, which doesn't work with sorted TStrings/TStringList

I see, yes, sorry.

Zoran

  • Hero Member
  • *****
  • Posts: 1652
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: ignoring a function result
« Reply #41 on: February 24, 2021, 10:21:01 am »
BTW - Zoran, did you see this...?

Yes, I added OnShowHint to (DB)DateTimePicker. I'll see about other events. I don't think that this needs backporting to fixes, though.

 

TinyPortal © 2005-2018