I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?
相关问题
- Multiple sockets for clients to connect to
- Do the Java Integer and Double objects have unnece
- Keeping track of variable instances
- What does an “empty line with semicolon” mean in C
- What is the best way to do a search in a large fil
Pass address of a function as parameter to another function as shown below
Also we can pass function as parameter using function pointer
Declaration
A prototype for a function which takes a function parameter looks like the following:
This states that the parameter
f
will be a pointer to a function which has avoid
return type and which takes a singleint
parameter. The following function (print
) is an example of a function which could be passed tofunc
as a parameter because it is the proper type:Function Call
When calling a function with a function parameter, the value passed must be a pointer to a function. Use the function's name (without parentheses) for this:
would call
func
, passing the print function to it.Function Body
As with any parameter, func can now use the parameter's name in the function body to access the value of the parameter. Let's say that func will apply the function it is passed to the numbers 0-4. Consider, first, what the loop would look like to call print directly:
Since
func
's parameter declaration says thatf
is the name for a pointer to the desired function, we recall first that iff
is a pointer then*f
is the thing thatf
points to (i.e. the functionprint
in this case). As a result, just replace every occurrence of print in the loop above with*f
:From http://math.hws.edu/bridgeman/courses/331/f05/handouts/c-c++-notes.html
You need to pass a function pointer. The syntax is a little cumbersome, but it's really powerful once you get familiar with it.
Since C++11 you can use the functional library to do this in a succinct and generic fashion. The syntax is, e.g.,
where
bool
is the return type here of a one-argument function whose first argument is of typeint
.I have included an example program below:
Sometimes, though, it is more convenient to use a template function:
This question already has the answer for defining function pointers, however they can get very messy, especially if you are going to be passing them around your application. To avoid this unpleasantness I would recommend that you typedef the function pointer into something more readable. For example.
Declares a function that returns void and takes no arguments. To create a function pointer to this type you can now do:
For a function that returns an int and takes a char you would do
and to use it
There are libraries that can help with turning function pointers into nice readable types. The boost function library is great and is well worth the effort!
is so much nicer than the above.
Functions can be "passed" as function pointers, as per 6.7.6.3p8: "A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1. ". For example, this:
is equivalent to this: