Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Closure (computer programming)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Callbacks (C) === Some [[C (programming language)|C]] libraries support [[Callback (computer programming)|callbacks]]. This is sometimes implemented by providing two values when registering the callback with the library: a function pointer and a separate <code>void*</code> pointer to arbitrary data of the user's choice. When the library executes the callback function, it passes along the data pointer. This enables the callback to maintain state and to refer to information captured at the time it was registered with the library. The idiom is similar to closures in functionality, but not in syntax. The <code>void*</code> pointer is not [[Type safety|type safe]] so this C idiom differs from type-safe closures in C#, Haskell or ML. Callbacks are used extensively in [[graphical user interface]] (GUI) [[widget toolkit]]s to implement [[event-driven programming]] by associating general functions of graphical widgets (menus, buttons, check boxes, sliders, spinners, etc.) with application-specific functions implementing the specific desired behavior for the application. ====Nested function and function pointer (C)==== With a [[GNU Compiler Collection]] (GCC) extension, a nested function<ref>{{cite web |url = https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html |title = Nested functions}}</ref> can be used and a function pointer can emulate closures, provided the function does not exit the containing scope. The next example is invalid because <code>adder</code> is a top-level definition (depending on compiler version, it could produce a correct result if compiled with no optimizing, i.e., at <code>-O0</code>): <syntaxhighlight lang="c"> #include <stdio.h> typedef int (*fn_int_to_int)(int); // type of function int->int fn_int_to_int adder(int number) { int add (int value) { return value + number; } return &add; // & operator is optional here because the name of a function in C is a pointer pointing on itself } int main(void) { fn_int_to_int add10 = adder(10); printf("%d\n", add10(1)); return 0; } </syntaxhighlight> But moving <code>adder</code> (and, optionally, the <code>typedef</code>) in <code>main</code> makes it valid: <syntaxhighlight lang="c"> #include <stdio.h> int main(void) { typedef int (*fn_int_to_int)(int); // type of function int->int fn_int_to_int adder(int number) { int add (int value) { return value + number; } return add; } fn_int_to_int add10 = adder(10); printf("%d\n", add10(1)); return 0; } </syntaxhighlight> If executed this now prints <code>11</code> as expected.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Closure (computer programming)
(section)
Add topic