Function Pointer

A function pointer is a type of pointer in the C and C++ programming languages. A function pointer, when dereferenced, calls a function with arguments placed after the dereferenced pointer just like a normal function. Function pointers are of the type the function returns.

An Example in C

Suppose we have a linked list containing integer values. We want to perform two operations on this list: 1) sum up all values, and 2) calculate the product of all values:
  #include <stdio.h>  int sum = 0;             /* store sum */ int product = 1;         /* store product */  void fsum(int value) { sum += value; }  void fproduct(int value) { product *= value; }  void map_function_on_list(list *L, void (*functionptr)(int)) {   listnode *node;   node = L->first;   while (node != NULL)   {     functionptr(node->value); /* call function pointer */     node = node->next;   } }  int main() {   list *L;    ... fill list with values ...      map_function_on_list(L, fsum);     /* calculate the sum */   map_function_on_list(L, fproduct); /* calculate product */    printf("Sum: %d\nProduct %d\n", sum, product); /* display results */ }  
Clearly, function pointers provide a powerful mechanism for programming. Imagine rewriting the above code without function pointers. The fsum() and fproduct() function would both require loops iterating through the linked list. What if we also want to have functions that subtract, divide, find the max/min value, etc? Function pointers are the clear choice of implementing such functions.

Function Values

Similar to function pointers, function values provide a mechanism for pass functions as arguments. In certain languages, such as ML, functions can be stored (or mapped to) values. This allows passing expressions as arguments. To demonstrate this, the following example determines if the elements in a list are even or odd:
  fun map(F, nil) = nil   | map(F, x::xs) = F(x)::map(F, xs);  map(fn x => x mod 2, [1,2,3,4,5,6,7,8,9]);  
The function map() takes a value and a list. In this case, the value we pass is a function, specificially x mod 2. This function is recursively applied to each element in the list.

External Link

 

<< PreviousWord BrowserNext >>
lord of the isles
malabar whistling thrush
streeterville
antarctic flora
spookyhorse
holosuite
beat frequency oscillator
wicker park
scala
coding strand
sri lanka whistling thrush
edison park, chicago
hummingbirds (venus hum)
pripyat, ukraine
big beautiful sky (venus hum)
homestar runner (character)
andersonville, chicago
switched on christmas (venus hum)
horton plains national park
pom pom
venus hum (venus hum)
heaven (movie)
strong sad
topix
hayden panettiere
function object
bubs
united state of electronica
essec
john comyn
u.s.e. ep (united state of electronica)
vaala
psychical
public sphere
bryant denny stadium
7 (enrique iglesias album)
heptameron
pierre mchain
skne regional council
flag of somalia
tbd devastator
kingdom hearts 2
rectory
university of alabama at birmingham