C Programming
Function Parameters
Coding Techniques
Advanced Programming
Software Development

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Passing a function as a parameter in C programming enables higher levels of abstraction such as callbacks, event handlers, and facilitating code reuse. This action is carried out through the use of function pointers, a feature supported in C that allows functions to be referenced and invoked dynamically. In this article, we will delve into how to pass functions as parameters using function pointers, illustrating this concept with detailed examples and explanations.

Understanding Function Pointers

A function pointer is essentially a variable that holds the address of a function that can be called through this pointer as though it was the function itself. To use a function pointer, you must declare it with the signature (return type and parameter list) of the function it is intended to point to.

Syntax:

c
return_type (*pointer_name)(parameter_list);

Example Declaration:

c
void (*fun_ptr)(int);

Here, fun_ptr is a pointer to a function that takes an int as an argument and returns void.

Passing Function Pointers as Arguments

To pass a function as a parameter, you need to declare your function such that it takes a function pointer as an argument.

Example Usage:

This example demonstrates how to create a function pointer to pass as an argument.

Function Declaration and Definition

Here is a simple function that we will reference via a function pointer:

c
void greet(char *name) {
    printf("Hello, %s!\n", name);
}

We now need a function that can accept a function pointer:

c
void process_greeting(void (*greet_func)(char*), char *name) {
    (*greet_func)(name);  // Call the function through the pointer
}

Calling process_greeting

Here's how you use the function:

c
1int main() {
2    void (*func_ptr)(char*) = greet; // Initialize pointer with the address of `greet`
3    process_greeting(func_ptr, "Alice"); // Passing pointer to function as an argument
4    return 0;
5}

Table Summarization

Given below is a table summarizing the different parts of using function pointers:

ComponentDescription
Declarationreturn_type (*pointer_name)(parameter_list);
Initializationpointer_name = &function_name;
Function Argumentvoid function_using_pointer(return_type (*func)(params), other_params)
Invocation(*pointer_name)(arguments);

Benefits and Uses

Passing functions as parameters using pointers can lead to versatile and reusable code architectures. Function pointers are prevalent in event-driven programming and designing generic libraries where actions can be customized without changing the core code.

  1. Callbacks: Enables asynchronous function executions.
  2. Event handlers: Used in GUI frameworks and system interrupts.
  3. Generic sorting and searching algorithms: C standard library functions, like qsort and bsearch, use function pointers.

Conclusion

Function pointers in C offer powerful tools for managing dynamic function invocation, which broadens the functionality and flexibility of software design. Understanding and using these pointers appropriately can yield substantial benefits in creating adaptive and intelligent C applications.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.