Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Higher-order function
(section)
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!
====Function pointers==== [[Function pointer]]s in languages such as [[C (programming language)|C]], [[C++]], [[Fortran]], and [[Pascal (programming language)|Pascal]] allow programmers to pass around references to functions. The following C code computes an approximation of the integral of an arbitrary function: <syntaxhighlight lang="c"> #include <stdio.h> double square(double x) { return x * x; } double cube(double x) { return x * x * x; } /* Compute the integral of f() within the interval [a,b] */ double integral(double f(double x), double a, double b, int n) { int i; double sum = 0; double dt = (b - a) / n; for (i = 0; i < n; ++i) { sum += f(a + (i + 0.5) * dt); } return sum * dt; } int main() { printf("%g\n", integral(square, 0, 1, 100)); printf("%g\n", integral(cube, 0, 1, 100)); return 0; } </syntaxhighlight> The [[qsort]] function from the C standard library uses a function pointer to emulate the behavior of a higher-order function.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)