All Topics  
Function pointer

 

   Email Print
   Bookmark   Link






 

Function pointer



 
 
A function pointer is a type of pointer in C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
, C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
, D, and other C-like programming languages. When dereferenced
Dereference operator

The dereference operator or indirection operator, "*", is a unary operator found in C -like languages that include pointer variables. It operates on a pointer variable, and returns an value equivalent to the value at the pointer address....
, a function pointer invokes a function
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
, passing it zero or more arguments just like a normal function. In programming languages like C, function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

Function objects, or functors, are similar to function pointers, and can be used in similar ways.






Discussion
Ask a question about 'Function pointer'
Start a new discussion about 'Function pointer'
Answer questions from other users
Full Discussion Forum



Encyclopedia


A function pointer is a type of pointer in C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
, C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
, D, and other C-like programming languages. When dereferenced
Dereference operator

The dereference operator or indirection operator, "*", is a unary operator found in C -like languages that include pointer variables. It operates on a pointer variable, and returns an value equivalent to the value at the pointer address....
, a function pointer invokes a function
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
, passing it zero or more arguments just like a normal function. In programming languages like C, function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

Function objects, or functors, are similar to function pointers, and can be used in similar ways. A functor is an object of a class type that implements the function-call operator, allowing the object to be used within expressions using the same syntax as a function call. Functors are more powerful than simple function pointers, being able to contain their own data values, and allowing the programmer to emulate closures
Closure (computer science)

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
, among other uses.

Many "pure" object-oriented languages (such as Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
) do not support function pointers. Something similar can be implemented in these kinds of languages, though, using references
Reference (computer science)

In computer science, a reference is an object containing information about how to locate and access the particular data item, as opposed to containing the data itself....
 to interfaces
Protocol (object-oriented programming)

In object-oriented programming, a protocol or interface is what or how unrelated object use to communication with each other. These are definitions of method s and values which the objects agree upon in order to cooperate....
 that define a single member function. Microsoft .NET languages such as C# and Visual Basic .NET
Visual Basic .NET

Visual Basic , formerly called Visual Basic .NET , is an object-oriented programming computer language that can be viewed as an evolution of Microsoft Visual Basic implemented on the .NET Framework....
 implement type-safe
Type safety

In computer science, type safety is a property of some programming languages that is defined differently by different communities, but most definitions involve the use of a type system to prevent certain erroneous or undesirable program behavior ....
 function pointers with delegates
Delegate (.NET)

A delegate is a form of Type safety function pointer used by the .NET Framework. Delegates specify a Method to call and optionally an Object to call the method on....
.

In other languages that support first-class function
First-class function

In computer science, a programming language is said to support first-class functions if it treats function s as first-class objects. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning the...
s, functions are regarded as data, and can be passed, returned, and created dynamically directly by other functions, eliminating the need for function pointers.

Extensively using function pointers to call functions may produce a slow-down for the code on modern processors, because branch prediction may not be able to figure out where to branch to (it depends on the value of the function pointer at run time) although this effect can be overstated as it is often amply compensated for by significantly reduced non indexed table lookups.

Method pointers

C++ is object-oriented, so classes can have method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
s. Non-static member functions (instance methods) have an implicit parameter (the this
This (computer science)

In many object-oriented programming programming languages, this is a keyword that is used in instance methods to refer to the object on which they are working....
 pointer) which is the pointer to the object it is operating on, so the type of the object must be included as part of the type of the function pointer. The method is then used on an object of that class by using one of the "pointer-to-member" operators: .* or ->* (for an object or a pointer to object, respectively).

Although function pointers in C and C++ can be implemented as simple addresses, so that typically sizeof(Fx)

sizeof(void *), member pointers in C++ are often implemented as "fat pointers", typically two or three times the size of a simple function pointer, in order to deal with virtual inheritance
Virtual inheritance

In the C++ programming language, virtual inheritance is a kind of Inheritance that solves some of the problems caused by multiple inheritance by clarifying ambiguity over which ancestor class members to use....
 (see also virtual function
Virtual function

In object-oriented programming, a virtual function or virtual method is one whose behavior can be Method overriding within an inheriting class by a function with the same Method signature....
).

External links

  • , Why can't void * be used as a generic function pointer in C
  • , C++ documentation and tutorials
  • , pointer basics
  • , a Guide to C/C++ function pointers, callbacks
    Callback (computer science)

    In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level abstraction layer to call a subroutine defined in a higher-level layer....
    , and functors
    Function object

    A function object, also called a functor, functional or functionoid, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function , usually with the same syntax....
  • , CodeProject article by Don Clugston.
  • , things not to do with function pointers, some information on using Functionoids.