C Language: Pass a function as a parameter in C language?
How to pass a function as a parameter in C?
In C language, it is possible to pass a function as a parameter to another function using function pointers.
Here is an example of how to pass a function as a parameter in C:
Code:
#include <stdio.h>
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
// Function to subtract two numbers
int subtract(int a, int b) {
return a - b;
}
// Function that accepts a function pointer as a parameter
int calculate(int (*operation)(int, int), int a, int b) {
return operation(a, b);
}
int main() {
int x = 100, y = 200;
int sum = calculate(add, x, y);
int diff = calculate(subtract, x, y);
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
return 0;
}
Output:
Sum: 300 Difference: -100
In the above example, there are two functions add() and subtract() that perform addition and subtraction of two numbers, respectively. We also have a function calculate() that accepts a function pointer operation as a parameter, along with two integers a and b. Using the function pointer operation, calculate() calls operation() with the given integers a and b.
In the main() function, we pass the add() and subtract() functions as parameters to the calculate() function using their function pointers. The calculate() function then calls the appropriate function based on the function pointer passed to it. Finally, the results are printed using printf() function.
Contribute your code and comments through Disqus.
C Programming: Tips of the Day
C Programming - How do you pass a function as a parameter in C?
Declaration
A prototype for a function which takes a function parameter looks like the following:
void func ( void (*f)(int) );
This states that the parameter f will be a pointer to a function which has a void return type and which takes a single int parameter. The following function (print) is an example of a function which could be passed to func as a parameter because it is the proper type:
void print ( int x ) { printf("%d\n", x); }
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:
func(print);
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:
for ( int ctr = 0 ; ctr < 5 ; ctr++ ) { print(ctr); }
Since func's parameter declaration says that f is the name for a pointer to the desired function, we recall first that if f is a pointer then *f is the thing that f points to (i.e. the function print in this case). As a result, just replace every occurrence of print in the loop above with *f:
void func ( void (*f)(int) ) { for ( int ctr = 0 ; ctr < 5 ; ctr++ ) { (*f)(ctr); } }
Ref : https://bit.ly/3skw9Um
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises