FACTOID # 28: Mexico has the most Jehovah's Witnesses per capita in the OECD.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Function pointer

A function pointer is a type of pointer in C, C++, D, and other C-like programming languages. When dereferenced, a function pointer invokes a function, passing it zero or more arguments just like a normal function. In programming languages like C, function pointers can be used to simplify code, such as replacing large switch statements. It has been suggested that Software pointer be merged into this article or section. ... C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ... D is an object-oriented, imperative system programming language designed by Walter Bright of Digital Mars as a re-engineering of C/C++. He has done this by re-designing many C++ features, and borrowing ideas from other programming languages. ... The dereference operator or indirection operator, *, is a unary operator found in C-like languages that include pointer variables. ... In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. ... In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (e. ...


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, among other uses. A function object, often called a functor, 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. ... In programming languages, a closure is a function that refers to free variables in its lexical context. ...


Many "pure" object-oriented languages (such as Java) do not support function pointers. Something similar can be implemented in these kinds of languages, though, using references to interfaces that define a single member function. Microsoft .NET languages such as C# and Visual Basic .NET implement type-safe function pointers with delegates. “Java language” redirects here. ... This article discusses a general notion of reference in computing. ... In computer sciences object-oriented programming, a protocol (Java: interface) is what or how unrelated objects use to communicate with each other. ... Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods or static methods) or with an object (called instance methods). ... Microsoft . ... The title given to this article is incorrect due to technical limitations. ... Visual Basic . ... In computer science, a programming language is type safe when the language does not permit the programmer to treat a value as a type to which it does not belong. ... A delegate is a form of type-safe function pointer used in the . ...


In other languages that support first-class functions, functions are regarded as data, and can be passed, returned, and created dynamically directly by other functions, eliminating the need for function pointers. In computer science, a programming language is said to support first-class functions if it treats functions as first-class objects. ...


Examples in C

This example demonstrates the use of function pointers. Here a function pointer new_function is declared, the address of the function named my_function() is assigned to it, and the function is then invoked by dereferencing the pointer.

 #include <stdio.h> static int my_function(int a) { printf("my_function: %dn", a); return (2*a + 3); } int main(void) { int (*new_function)(int) = my_function; int x; x = (*new_function)(10); printf("main: %dn", x); return 0; } 

Note that the syntax (*fp)(arg) is used to invoke the function indirectly through function pointer fp. C syntax also allows the simpler fp(arg) to be used, since the fp subexpression evaluates to the address of a function whether it is a function name or a pointer to a function.


The next example shows how function pointers can be used as parameters to other functions. As in the previous example, a function named function() is defined which will be called through a function pointer. A function named caller() accepts as parameters a function pointer and an integer, which will be passed as arguments to the function that will be called by caller(). The caller's first parameter will accept the name (address of) any function that matches the prototype of the function pointer.

 #include <stdio.h> static void function(int a) { printf("my_function: %dn", a); } static void caller(void (*new_function)(int), int p) { new_function(p); } int main(void) { caller(function, 10); return 0; } 

The third example also shows how function pointers can be used as parameters to other functions. Here a function f() is passed to function integ() which approximates an integral evaluated over an interval, textstyleint_{a}^{b} f(x);dx, calling the function f(x) for each value of x. Any function taking a single double argument and returning a double result can be passed to integ() for evaluation. The integral of f(x) from a to b is the area above the x-axis and below the curve y = f(x), minus the area below the x-axis and above the curve, for x in the interval [a,b]. Integration is a core concept of advanced mathematics, specifically...

 double integ(double a, double b, double (*f)(double)) { double sum = 0.0; double x; int n; // Evaluate integral{a,b} f(x) dx for (n = 0; n <= 100; n++) { x = (n/100.0)*(b-a) + a; sum += (*f)(x) * (b-a)/101.0; } return sum; } 

For defining an array of function pointers, it might be useful to use a typedef to declare a new type for your function pointer. typedef is a keyword in the C Programming Language. ...

 #include <math.h> typedef double (*Fx)(double); int main(void) { Fx f[3]; f[0] = cos; f[1] = sin; f[2] = tan; printf("From 0 to pi/4:n"); printf("t integ of cos = %gn", integ(0, M_PI/4, f[0])); printf("t integ of sin = %gn", integ(0, M_PI/4, f[1])); printf("t integ of tan = %gn", integ(0, M_PI/4, f[2])); return 0; } 

External links

  • Pointer Tutorials, C++ documentation and tutorials
  • Function Pointer Tutorials, a Guide to C/C++ function pointers, callbacks, and functors

  Results from FactBites:
 
Closure (computer science) - Wikipedia, the free encyclopedia (1380 words)
Closures are commonly used in functional programming to defer calculation, to hide state, and as arguments to higher-order functions.
Closures are typically implemented with a special data structure that contains a pointer to the function code, plus a representation of the function's lexical environment (i.e., the set of available variables and their values) at the time when the function was created.
Closures typically appear in languages in which functions are first-class values—in other words, such languages allow functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.