Logo

How do you pass a function as a parameter in C?

In C, you can pass a pointer to a function as a parameter. Unlike higher-level languages that might offer first-class function types or “closures,” C requires you to work with pointers that reference an existing function’s entry point in memory. Below is a practical explanation and examples of how to do it.

1. Declaring a Function Pointer

A function pointer declaration specifies:

  1. The return type of the function.
  2. The parameter types.
  3. A pointer variable name wrapped in parentheses, followed by the parameter list.

Example

// A function pointer type: returns int, takes (int, int) int (*funcPtr)(int, int);

This statement says funcPtr is a pointer to a function that returns an int and takes two int parameters.

2. Assigning a Function to the Pointer

If you have a function like:

int add(int a, int b) { return a + b; }

you can make your funcPtr point to it:

funcPtr = add;

Or directly initialize it:

int (*funcPtr)(int, int) = add;

3. Passing a Function Pointer as a Parameter

To pass a function pointer into another function, you simply define a parameter for that pointer type in your function’s signature.

Step-by-Step Example

  1. Define the Function

    int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; }
  2. Function That Accepts a Function Pointer

    // 'operation' is a pointer to a function taking (int, int) -> int int calculate(int x, int y, int (*operation)(int, int)) { return operation(x, y); }
  3. Call it

    #include <stdio.h> int main(void) { // Pass 'add' as a parameter int result = calculate(5, 3, add); printf("Result of add: %d\n", result); // Output: 8 // Pass 'subtract' as a parameter result = calculate(5, 3, subtract); printf("Result of subtract: %d\n", result); // Output: 2 return 0; }
    • In both calls, calculate uses whichever function pointer you provide.

4. Example with Arrays of Function Pointers

Sometimes you want multiple operations (like “add,” “subtract,” “multiply”) in an array. You could do:

#include <stdio.h> int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a * b; } int main(void) { // An array of function pointers int (*ops[3])(int, int) = { add, sub, mul }; printf("%d\n", ops[0](10, 5)); // calls add printf("%d\n", ops[1](10, 5)); // calls sub printf("%d\n", ops[2](10, 5)); // calls mul return 0; }

This approach is handy for building simple “dispatch tables” or “callback arrays.”

5. Tips and Best Practices

  1. Use typedef for Readability
    • Declaring function pointers inline can be verbose. A typedef can simplify usage:
      typedef int (*operation_t)(int, int); int calculate(int x, int y, operation_t op) { return op(x, y); }
  2. Always Match Signatures
    • Ensure the function pointer’s signature (return type and parameters) matches exactly with the function you’re assigning.
  3. Check for NULL
    • In production code, ensure you’re not accidentally calling a NULL or uninitialized function pointer.
  4. Compile & Run
    • Function pointers are powerful but error-prone if used incorrectly. Using a debug build or tools like valgrind can help catch mistakes.

Conclusion

To pass a function as a parameter in C, you:

  1. Declare a function pointer with the correct signature (return type, parameters).
  2. Assign your function (e.g., add, subtract) to that pointer.
  3. Use the pointer as a parameter in another function’s signature, and call it inside that function (operation(x, y)).

Mastering function pointers is key for callbacks, dynamic dispatch, and advanced patterns in C. With practice, you’ll be able to write flexible, reusable code—especially for library functions or plugin-based architectures.

Further Learning

If you want to dive deeper into data structures, pointers, and algorithms—and master them for coding interviews—two recommended courses from DesignGurus.io are:

  1. Grokking Data Structures & Algorithms for Coding Interviews
    A solid exploration of fundamental data structures and the algorithms operating on them.

  2. Grokking the Coding Interview: Patterns for Coding Questions
    Learn recurring coding patterns that will save you time and stress during technical interviews, including those involving pointer manipulations.

By combining thorough knowledge of function pointers with robust algorithm skills, you’ll be well-prepared for writing sophisticated C programs and acing coding interviews.

CONTRIBUTOR
TechGrind