FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Function_pointer

Function pointer

Contents
Functors
PL/I
See also
References
External links

A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments just as in a normal function call. Such an invocation is also known as an "indirect" call, because the function is being invoked indirectly through a variable instead of directly through a fixed identifier or address.

Function pointers allow different code to be executed at runtime. They can also be passed to a function to enable callbacks.

Function pointers are supported by third-generation programming languages (such as PL/I, COBOL, Fortran, dBASE dBLfunc1 takes one double-precision (double) parameter and returns another double, and is assigned to a function which converts centimeters to inches.

  • \n\n// Function taking a function pointer as an argument\ndouble compute_sum(double (*funcp)(double), double lo, double hi) {\n double sum = 0.0;\n\n // Add values returned by the pointed-to function '*funcp'\n for (int i = 0; i <= 100; i++) {\n // Use the function pointer 'funcp' to invoke the function\n double x = i / 100.0 * (hi - lo) + lo;\n double y = funcp(x);\n sum += y;\n }\n return sum / 101.0 * (hi - lo);\n}\n\ndouble square(double x) {\n return x * x;\n}\n\nint main(void) {\n double sum;\n\n // Use standard library function 'sin()' as the pointed-to function\n sum = compute_sum(sin, 0.0, 1.0);\n printf(\"sum(sin): %g\\n\", sum);\n\n // Use standard library function 'cos()' as the pointed-to function\n sum = compute_sum(cos, 0.0, 1.0);\n printf(\"sum(cos): %g\\n\", sum);\n\n // Use user-defined function 'square()' as the pointed-to function\n sum = compute_sum(square, 0.0, 1.0);\n printf(\"sum(square): %g\\n\", sum);\n\n return 0;\n}\n"}}'>#include<math.h> #include<stdio.h> // Function taking a function pointer as an argument doublecompute_sum(double(*funcp)(double),doublelo,doublehi){ doublesum=0.0; // Add values returned by the pointed-to function '*funcp' for(inti=0;i<=100;i++){ // Use the function pointer 'funcp' to invoke the function doublex=i/100.0*(hi-lo)+lo; doubley=funcp(x); sum+=y; } returnsum/101.0*(hi-lo); } doublesquare(doublex){ returnx*x; } intmain(void){ doublesum; // Use standard library function 'sin()' as the pointed-to function sum=compute_sum(sin,0.0,1.0); printf("sum(sin): %g\n",sum); // Use standard library function 'cos()' as the pointed-to function sum=compute_sum(cos,0.0,1.0); printf("sum(cos): %g\n",sum); // Use user-defined function 'square()' as the pointed-to function sum=compute_sum(square,0.0,1.0); printf("sum(square): %g\n",sum); return0; }
  • Functors

    edit

    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 sometimes implemented as "fat pointers", typically two or three times the size of a simple function pointer, in order to deal with virtual methods and virtual inheritance.// This defines 'C', a class with similar static and member functions, // and then creates an instance called 'c' classC{ public: staticintStatic(charc); intMember(charc); }c;// C // This defines 'p', a pointer to 'C' and assigns the address of 'c' to it C*p=&c; // This assigns a pointer-to-'Static' to 'fn'. // Since there is no 'this', 'Fn' is the correct type; and 'fn' can be used as above. fn=&C::Static; // This defines 'm', a pointer-to-member-of-'C' with type 'Fn', // and assigns the address of 'C::Member' to it. // You can read it right-to-left like all pointers: // "'m' is a pointer to member of class 'C' of type 'Fn'" FnC::*m=&C::Member; // This uses 'm' to call 'Member' in 'c', assigning the result to 'cA' intcA=(c.*m)('A'); // This uses 'm' to call 'Member' in 'p', assigning the result to 'pA' intpA=(p->*m)('A'); // This defines 'Ref', a function that accepts a reference-to-'C', // a pointer-to-member-of-'C' of type 'Fn', and a 'char', // calls the function and returns the result intRef(C&r,FnC::*m,charc){ return(r.*m)(c); }// Ref(r, m, c) // This defines 'Ptr', a function that accepts a pointer-to-'C', // a pointer-to-member-of-'C' of type 'Fn', and a 'char', // calls the function and returns the result intPtr(C*p,FnC::*m,charc){ return(p->*m)(c); }// Ptr(p, m, c) // LEGACY: Note that to maintain existing code bases, the above definition style can still be used first; // then the original type can be defined in terms of it using the new style. // This defines 'FnC', a type of pointer-to-member-of-class-'C' of type 'Fn' typedefFnC::*FnC; // 'FnC' can be used wherever 'Fn C::*' can FnCfnC=&C::Member; intRefP(C&p,FnCm,charc);

    PL/I

    edit

    PL/I procedures can be nested, that is, procedure A may contain procedure B, which in turn may contain C. In addition to data declared in B, B can also reference any data declared in A, as long as it doesn’t override the definition. Likewise C can reference data in both A and B. Therefore, PL/I ENTRY variables need to contain context, to provide procedure C with the addresses of the values of data in B and A at the time C was called.

    See also

    edit

    References

    edit
    "The Function Pointer Tutorials". logo. Archived from the original on 2011-05-16. Retrieved 2011-04-13. Important note: A function pointer always points to a function with a specific signature! Thus all functions, you want to use with the same function pointer, must have the same parameters and return-type!
  • ↑ Abrahams, Paul (March 1978). The PL/I Programming Language (PDF). Courant Mathematics and Computing Laboratory, New York University. pp. 22–24. Retrieved April 4, 2025.
  • External links

    edit

    View original on Wikipedia | Back | FazBrowse Home