Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a function as a parameter in C

Tags:

c

function

I am trying to use a function as a parameter for another function. I want to get different computations depending on the function I pass. I tried implementing this by writing this program:
http://pastebin.com/CJfFarVa
Code:

The problem is I get this error over and over again:

trapeze.c:14:8: error: conflicting types for ‘exp2’
double exp2(int number);

trapeze.c: In function ‘main’:
trapeze.c:28:35: error: expected expression before ‘int’
exp_2 = trapeze(0, 1, n, exp2(int x));
                               ^
trapeze.c:29:35: error: expected expression before ‘int’
exp_1 = trapeze(0, 1, n, exp1(int x));
                               ^
trapeze.c: At top level: 
trapeze.c:58:8: error: conflicting types for ‘exp2’
double exp2(int number)

Note: I do not want to use pointers to functions.
Sorry if this is an easy question.

like image 885
Andy_A̷n̷d̷y̷ Avatar asked May 08 '26 12:05

Andy_A̷n̷d̷y̷


2 Answers

Change the call to trapeze:

exp_2 = trapeze(0, 1, n, exp2);
exp_1 = trapeze(0, 1, n, exp1);
like image 182
R Sahu Avatar answered May 10 '26 06:05

R Sahu


C is not a functional programming language, which means function is not a first-class object, therefore, you cannot pass function as argument of other functions, you have to use pointer to function.

like image 32
Lee Duhem Avatar answered May 10 '26 04:05

Lee Duhem