by Dinesh Thakur Category: Function and Pointer

A function may have another function as one of its parameters besides having parameters of other data types. A function is a derived type object; its type is derived from the type of data it returns. Like arrays, the name of a function also holds the address of the function.

Therefore, a function may be passed on to another function as its argument either by the name of function or by a pointer to a function. In both these cases we are simply passing on the address of the function as the argument. Since name of a function is a constant pointer to a function; another pointer, declared for the function, can be initialized by the name of the function. The declaration of a pointer has already been discussed earlier. The prototype of function with pointer to another function as its parameter may be declared as given below.

int Function (int (*)(int, int), int, int);

In the above prototype the first parameter is the pointer prototype to functions, which accept two integers as arguments and return an integer. The other two arguments are the types of parameters of the functions.

In Program, we declare a function with the name Function and of type int, i.e., returns an integer number. Pointer to other functions, i.e., int (*) (int, int) ,is one of its parameters. There are other three more parameters of type int: Two of these are for the functions, while the third may be used for modifying the output of functions to which the first argument points to. The first argument, i.e., the pointer is used to call two functions: Surface_area () and Perimeter (). The function surface_area () calculates the area of a rectangle, and the function Perimeter () calculates the sum of four sides of a rectangle. Let us consider Program pointer to functions as a parameter of a function.

#include <stdio.h>

int Function (int(*)(int,int),int, int, int); /*Above is the function prototype.*/
int Perimeter(int, int); //prototype of Perimeter()
int Surface_area (int, int); //prototype of Surface area()
    int K = 6, M = 5, j = 4;
    printf ("Surface_area and perimeter of a rectangle\n");
    printf("Surface_area *4 = %d\n", Function(Surface_area,K,M,j));
    printf("Perimeter *4 = %d\n", Function(Perimeter,K,M,j));
    return 0;
    //Below is the definitionof Function
   int Function (int(*pF)(int i,int n ),int k, int m, int j)
      return j* (*pF)(k,m);
   // Below is definitionof Surface_area()
  int Surface_area(int a, int b)
      return a*b ;
   // Below is definitionof function Perimeter()
   int Perimeter(int c, int d)
      return 2*(c+d);

Pointer to Functions as Parameter in C

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles