#include<stdio.h>
int areaOfRectangle(int,int);
int perimeter(int,int);
int main()
{
int l,b;
scanf(" %d %d",&l,&b);
printf("%d %d %d %d",l,b,areaOfRectangle(l,b),perimeter(l,b));
return 0;
}
int areaOfRectangle(int a,int b)
{
int area;
area=a*b;
return area;
}
int perimeter(int c,int d)
{
int meter;
meter=2(c+d);
return meter;
}
why this error:called object is not a function or function pointer at line: meter=2(c+d)?
Also, can I use the same variable a,b to pass in perimeter function?
Your code meter=2(c+d);should be changes as meter=2*(c+d);
You can use the same variable a,b to pass in perimeter function, A parameter is just a local variable.
In this statement:
int perimeter(int c,int d)
{
int meter;
meter=2(c+d);
return meter;
}
You'll obviously get a syntax error since the compiler won't detect 2(c+d) as you're trying to implicitly multiply 2 with (c + d). Rather, if you explicitly define 2 * (c + d) then it'll no longer show you any error.
One side tip, you don't need to use any local variables inside a function if you just want to return a simple return statement, rather you may use:
return 2 * (c + d);
The declaration is redundant thence.
Related
I have a quastion about C functions. Is there any possibility to do something like:
#define PRINT_SUM_OF_CONSTS() printSum(10, 5)
void printSum(int a, int b){
print("%d + %d = %d", a, b, a+b);
}
int main(){
void (*pFunc)(void);
pFunc = &PRINT_SUM_OF_CONSTS;
pFunc();
return 0;
}
What I need is to use function which takes two arguments, asign arguments to constants with a macro and use it as function without arguments. Is that somehow possible?
pFunc is a pointer to a function, you cannot create a pointer to a function call with specific parameters.
However, the solution is simple and does not require a macro - you simply create a wrapper function that calls the target function with the desired parameters:
#include <stdio.h>
void printSum(int a, int b)
{
printf("%d + %d = %d", a, b, a+b);
}
void printSumConst() { printSum(10, 5) ; }
int main()
{
void (*pFunc)(void) = printSumConst;
pFunc();
return 0;
}
I want to create a function that performs a function passed by parameter on a set of data. 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);
}
}
Source
This question already has the answer for defining function pointers, however they can get very messy, especially if you are going to be passing them around your application. To avoid this unpleasantness I would recommend that you typedef the function pointer into something more readable. For example.
typedef void (*functiontype)();
Declares a function that returns void and takes no arguments. To create a function pointer to this type you can now do:
void dosomething() { }
functiontype func = &dosomething;
func();
For a function that returns an int and takes a char you would do
typedef int (*functiontype2)(char);
and to use it
int dosomethingwithchar(char a) { return 1; }
functiontype2 func2 = &dosomethingwithchar
int result = func2('a');
There are libraries that can help with turning function pointers into nice readable types. The boost function library is great and is well worth the effort!
boost::function<int (char a)> functiontype2;
is so much nicer than the above.
Since C++11 you can use the functional library to do this in a succinct and generic fashion. The syntax is, e.g.,
std::function<bool (int)>
where bool is the return type here of a one-argument function whose first argument is of type int.
I have included an example program below:
// g++ test.cpp --std=c++11
#include <functional>
double Combiner(double a, double b, std::function<double (double,double)> func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Sometimes, though, it is more convenient to use a template function:
// g++ test.cpp --std=c++11
template<class T>
double Combiner(double a, double b, T func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Pass address of a function as parameter to another function as shown below
#include <stdio.h>
void print();
void execute(void());
int main()
{
execute(print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void f()) // receive address of print
{
f();
}
Also we can pass function as parameter using function pointer
#include <stdio.h>
void print();
void execute(void (*f)());
int main()
{
execute(&print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void (*f)()) // receive address of print
{
f();
}
Functions can be "passed" as function pointers, as per ISO C11 6.7.6.3p8: "A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1. ". For example, this:
void foo(int bar(int, int));
is equivalent to this:
void foo(int (*bar)(int, int));
I am gonna explain with a simple example code which takes a compare function as parameter to another sorting function.
Lets say I have a bubble sort function that takes a custom compare function and uses it instead of a fixed if statement.
Compare Function
bool compare(int a, int b) {
return a > b;
}
Now , the Bubble sort that takes another function as its parameter to perform comparison
Bubble sort function
void bubble_sort(int arr[], int n, bool (&cmp)(int a, int b)) {
for (int i = 0;i < n - 1;i++) {
for (int j = 0;j < (n - 1 - i);j++) {
if (cmp(arr[j], arr[j + 1])) {
swap(arr[j], arr[j + 1]);
}
}
}
}
Finally , the main which calls the Bubble sort function by passing the boolean compare function as argument.
int main()
{
int i, n = 10, key = 11;
int arr[10] = { 20, 22, 18, 8, 12, 3, 6, 12, 11, 15 };
bubble_sort(arr, n, compare);
cout<<"Sorted Order"<<endl;
for (int i = 0;i < n;i++) {
cout << arr[i] << " ";
}
}
Output:
Sorted Order
3 6 8 11 12 12 15 18 20 22
You need to pass a function pointer. The syntax is a little cumbersome, but it's really powerful once you get familiar with it.
typedef int function();
function *g(function *f)
{
f();
return f;
}
int main(void)
{
function f;
function *fn = g(f);
fn();
}
int f() { return 0; }
It's not really a function, but it is an localised piece of code. Of course it doesn't pass the code just the result. It won't work if passed to an event dispatcher to be run at a later time (as the result is calculated now and not when the event occurs). But it does localise your code into one place if that is all you are trying to do.
#include <stdio.h>
int IncMultInt(int a, int b)
{
a++;
return a * b;
}
int main(int argc, char *argv[])
{
int a = 5;
int b = 7;
printf("%d * %d = %d\n", a, b, IncMultInt(a, b));
b = 9;
// Create some local code with it's own local variable
printf("%d * %d = %d\n", a, b, ( { int _a = a+1; _a * b; } ) );
return 0;
}
i have a simple program like this :
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*add)(int *))
{
if (nb < 5)
f(nb, add(&nb));
return (nb);
}
int main() {
int b = 5;
int a = f(b, add);
printf("%d\n", a);
}
i want to call f recursively until nb become greater or equal to 5, but when i compile the program , the gcc compiler show something like this :
error: passing 'void' to parameter of incompatible type 'void (*)(int *)'
can anyone help me please?
I'm not completely certain as to what you are trying to do with your function, but I took a stab at what I think you might be attempting to do:
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*function)(int *))
{
function(&nb);
if (nb < 5)
f(nb, function);
return (nb);
}
int main() {
int b = 5;
int a = f(b, add);
printf("%d\n", a);
}
Now your function f accepts a function-pointer to a void function that takes an int * as an argument (I renamed this argument to function so it doesn't conflict with your existing function add). It proceeds to call said function and pass in a pointer to nb to function.
What you were doing was passing in the result of add(&nb) into the function f as an argument (which said function is a void so it does not return anything) instead of passing in a pointer to the function.
The line f(nb, add(&nb)); doesn't make sense. You need to call the function somewhere, but not on the same line as where you pass the function pointer on, recursively.
Overall the program doesn't make much sense. I have no idea what you are trying to do, perhaps something similar to this?
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
void f(int* nb, void (*add)(int *))
{
add(nb);
if (*nb < 5)
{
f(nb, add);
}
}
int main() {
int a = 5;
f(&a, add);
printf("%d\n", a);
}
Will print 6 since the add is called once. The name add as function parameter to f is also mighty confusing, so better come up with another name for it.
Passing add(&nb) as an argument passes the returned value of add with the argument &nb. The function signature for f requires a function pointer, which can be passed as:
f(nb, add)
Also, even if you were actually trying to pass the returned value of add, that would be undefined behavior since it is of type void.
As Christian pointed out in the comments, the problem is on the recursive call line:
f(nb, add(&nb));
If I understand correctly you are trying to increase nb using the function you receive as a pointer; here however you first pass nb unchanged (and as a value, not by reference), and then call the add function.
In the second argument the compiler is expecting a function pointer, but instead it receives the return value of the add(&nb) function call, which is void.
Also, in case nb is < 5 you need to return the recursive call.
The correct procedure would be:
#include<stdio.h>
void add(int *nb)
{
*nb += 1;
}
int f(int nb, void (*add)(int *))
{
if (nb < 5) {
add(&nb);
return f(nb, add);
}
return (nb);
}
int main() {
int b = 1;
int a = f(b, add);
printf("%d\n", a);
}
It almost seems like you're trying to use lazy evaluation, but C doesn't have that!
Is that possible to send two values to function and return both separately without using data structures such as arrays?
like this:
#include<stdio.h>
int f(int a,int b)
{
a*=2;
b*=2;
return ?????????
}
int main()
{
int x=5,y=10,k;
k=f(x,y) ?????????
printf("%d",k); ????????
}
You cannot directly return more than one item (where an item could be a structure containing multiple items within). However you can "pass by reference" if you're comfortable with pointers.
#include <stdio.h>
void f(int *a, int *b)
{
*a *= 2;
*b *= 2;
}
int main()
{
int x=5, y=10;
f(&x, &y);
printf("new x: %d, new y: %d", x, y);
}
See the results of this at http://ideone.com/p4Xiqv
No, its not possible to return more than one values without using any data structures. But, you can pass any number of arguments.
I want to create a function that performs a function passed by parameter on a set of data. 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);
}
}
Source
This question already has the answer for defining function pointers, however they can get very messy, especially if you are going to be passing them around your application. To avoid this unpleasantness I would recommend that you typedef the function pointer into something more readable. For example.
typedef void (*functiontype)();
Declares a function that returns void and takes no arguments. To create a function pointer to this type you can now do:
void dosomething() { }
functiontype func = &dosomething;
func();
For a function that returns an int and takes a char you would do
typedef int (*functiontype2)(char);
and to use it
int dosomethingwithchar(char a) { return 1; }
functiontype2 func2 = &dosomethingwithchar
int result = func2('a');
There are libraries that can help with turning function pointers into nice readable types. The boost function library is great and is well worth the effort!
boost::function<int (char a)> functiontype2;
is so much nicer than the above.
Since C++11 you can use the functional library to do this in a succinct and generic fashion. The syntax is, e.g.,
std::function<bool (int)>
where bool is the return type here of a one-argument function whose first argument is of type int.
I have included an example program below:
// g++ test.cpp --std=c++11
#include <functional>
double Combiner(double a, double b, std::function<double (double,double)> func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Sometimes, though, it is more convenient to use a template function:
// g++ test.cpp --std=c++11
template<class T>
double Combiner(double a, double b, T func){
return func(a,b);
}
double Add(double a, double b){
return a+b;
}
double Mult(double a, double b){
return a*b;
}
int main(){
Combiner(12,13,Add);
Combiner(12,13,Mult);
}
Pass address of a function as parameter to another function as shown below
#include <stdio.h>
void print();
void execute(void());
int main()
{
execute(print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void f()) // receive address of print
{
f();
}
Also we can pass function as parameter using function pointer
#include <stdio.h>
void print();
void execute(void (*f)());
int main()
{
execute(&print); // sends address of print
return 0;
}
void print()
{
printf("Hello!");
}
void execute(void (*f)()) // receive address of print
{
f();
}
Functions can be "passed" as function pointers, as per ISO C11 6.7.6.3p8: "A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1. ". For example, this:
void foo(int bar(int, int));
is equivalent to this:
void foo(int (*bar)(int, int));
I am gonna explain with a simple example code which takes a compare function as parameter to another sorting function.
Lets say I have a bubble sort function that takes a custom compare function and uses it instead of a fixed if statement.
Compare Function
bool compare(int a, int b) {
return a > b;
}
Now , the Bubble sort that takes another function as its parameter to perform comparison
Bubble sort function
void bubble_sort(int arr[], int n, bool (&cmp)(int a, int b)) {
for (int i = 0;i < n - 1;i++) {
for (int j = 0;j < (n - 1 - i);j++) {
if (cmp(arr[j], arr[j + 1])) {
swap(arr[j], arr[j + 1]);
}
}
}
}
Finally , the main which calls the Bubble sort function by passing the boolean compare function as argument.
int main()
{
int i, n = 10, key = 11;
int arr[10] = { 20, 22, 18, 8, 12, 3, 6, 12, 11, 15 };
bubble_sort(arr, n, compare);
cout<<"Sorted Order"<<endl;
for (int i = 0;i < n;i++) {
cout << arr[i] << " ";
}
}
Output:
Sorted Order
3 6 8 11 12 12 15 18 20 22
You need to pass a function pointer. The syntax is a little cumbersome, but it's really powerful once you get familiar with it.
typedef int function();
function *g(function *f)
{
f();
return f;
}
int main(void)
{
function f;
function *fn = g(f);
fn();
}
int f() { return 0; }
It's not really a function, but it is an localised piece of code. Of course it doesn't pass the code just the result. It won't work if passed to an event dispatcher to be run at a later time (as the result is calculated now and not when the event occurs). But it does localise your code into one place if that is all you are trying to do.
#include <stdio.h>
int IncMultInt(int a, int b)
{
a++;
return a * b;
}
int main(int argc, char *argv[])
{
int a = 5;
int b = 7;
printf("%d * %d = %d\n", a, b, IncMultInt(a, b));
b = 9;
// Create some local code with it's own local variable
printf("%d * %d = %d\n", a, b, ( { int _a = a+1; _a * b; } ) );
return 0;
}