How to Pass a Function As a Parameter to Another Function in C: A Tutorial
Airfare Daily Deals eCigarettes Eyeglasses Hotels Jewelry Online Backup Online Dating Online Printing Online Tickets Skin Care Textbook Rentals Vitamins Web Hosting Weddings
Find thousands of shopping-related forums

How to Pass a Function As a Parameter to Another Function in C: A Tutorial

While writing a large program sometime it becomes useful to pass a function as an argument to another function. Unlike the case of passing simple variables, passing functions is not direct and require some knowledge of syntax. This article describes the method by discussing passing functions of a single variable, multiple variables and pointers.

It is a very good practice to use functions in programming because of mainly two reasons; first, functions save a lot of rewriting of the same portion of code and second, functions help dividing the whole program into small sections, each dedicated to perform a specific operation, making the program easy to conceive. Normally we use different values or pointers of variables as parameters of a function but sometimes it becomes essential to pass a function as an argument to another program in a very large program. For example, when we try to solve differential equations of boundary value kind or find complex zeros of an integral equation we need to use functions as parameters to keep the program handy and easily understandable. Let us try to learn this technique by first considering a function with single argument.

Passing a Function with a Single Parameter

This involves three major steps:

  1. Function prototype declaration
  2. Function call
  3. Function body

Let us understand these by help of the following simple example.

  1. The prototype of the function “func” used here states that it takes two arguments. The parameter “f” is a pointer to a function which takes a single “double” variable as parameter and has “void” return type.
  2. Function call is very easy: the first parameter is the name of the function (here “square” or “cube”) to be passed and the second is a variable of “double” type (here “x”).
  3. Function body is same like any other function with the parameter “f” which can be called within it.

The program written here finds the square and cube of a number taken as input from user. This is done by passing the functions “square” and “cube” as parameters to “func” consecutively.

Passing a Function with Multiple Parameters

A generalization to multiple parameters is quiet straightforward. We need to increase the number of variables in the prototype declaration as well as function body as shown in the following program.

Here “func” has a parameter “f” which is a pointer to another function that takes two integers as arguments and has a return type of “int”. The program takes two integers as input and passes them to the function “mult” which returns the integer product of the two inputs. Though this program only uses two parameters, they can be increased at will.

Use of Call by Reference

In the previous two examples we have used the call by value procedure (which simply means passing the variables itself) for passing arguments to functions. These kinds of functions can return only one value but when we need to get several outputs simultaneously from a single function then we have to use the method of call by reference which uses pointers (or the addresses of the variables) as arguments of the function.

This simple program finds the sum of two integers but here the address of variable “a” is passed to “func” instead of the value and the sum is stored in “a” inside “func” so that the result is directly available in “main”. It is also a good habit to use call by reference, i.e. pointers since it makes the program run faster.

Thus we have learned how to pass a function as a parameter of another function in C.

Need an answer?
Get insightful answers from community-recommended
in Computer Programming & Languages on Knoji.
Would you recommend this author as an expert in Computer Programming & Languages?
You have 0 recommendations remaining to grant today.
Comments (0)