Friday, August 7, 2009

POINTERS OF FUNCTIONS

Functions like data items have addresses.A functions address is the memory at which the stored machine language code for the function begin normally its neither important nor useful for user to know the address.But it can be useful to programe.
For,example,It's possible to write a function that takes the address of another function as an argument.That enables the first function to find the second function adn run it.This approach is more awkward than simply having the first function call the second one directly,but it leaves open the possibaity of passing different function address at a different times.
The machanism involves few steps such as.
1.Take the address of function.
2.Declare a pointer to a function to invoke the Function.
OBTAING THE ADDRESS OF A FUNCTIONS:
Taking the address of a function is simple just use the function name without trailing parenthneses For wxample if SUM() is the function then sumis the address of the function.So as to pass a function as an argument pass the function name.
process(think); //passes adress of think() to process() //
thoughr(thunk()) // passes return values of think () tothrough//
DECLARING POINTER TO A FUNCTION :
Double (*pf) (int); //pf points to a function that takes one int argument and thatreturns type double//
Using a pointer to invoke a function:
All that we have to do is to to just use pointer name to call the function.

No comments:

Post a Comment