Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Declaration</strong></p> <p>A prototype for a function which takes a function parameter looks like the following:</p> <pre><code>void func ( void (*f)(int) ); </code></pre> <p>This states that the parameter <code>f</code> will be a pointer to a function which has a <code>void</code> return type and which takes a single <code>int</code> parameter. The following function (<code>print</code>) is an example of a function which could be passed to <code>func</code> as a parameter because it is the proper type:</p> <pre><code>void print ( int x ) { printf("%d\n", x); } </code></pre> <p><strong>Function Call</strong></p> <p>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:</p> <pre><code>func(print); </code></pre> <p>would call <code>func</code>, passing the print function to it.</p> <p><strong>Function Body</strong></p> <p>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:</p> <pre><code>for ( int ctr = 0 ; ctr &lt; 5 ; ctr++ ) { print(ctr); } </code></pre> <p>Since <code>func</code>'s parameter declaration says that <code>f</code> is the name for a pointer to the desired function, we recall first that if <code>f</code> is a pointer then <code>*f</code> is the thing that <code>f</code> points to (i.e. the function <code>print</code> in this case). As a result, just replace every occurrence of print in the loop above with <code>*f</code>:</p> <pre><code>void func ( void (*f)(int) ) { for ( int ctr = 0 ; ctr &lt; 5 ; ctr++ ) { (*f)(ctr); } } </code></pre> <p>From <a href="http://math.hws.edu/bridgeman/courses/331/f05/handouts/c-c++-notes.html" rel="noreferrer">http://math.hws.edu/bridgeman/courses/331/f05/handouts/c-c++-notes.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload