110 likes | 202 Views
Functions as parameters general issue c++ java. What?. Typically programmers use data structures as the means to give specific definition to a general algorithm. Parameters allow us to abstract from examples how the various uses can be viewed as being similar. Area of a square = s 2
E N D
What? • Typically programmers use data structures as the means to give specific definition to a general algorithm. • Parameters allow us to abstract from examples how the various uses can be viewed as being similar. • Area of a square = s2 • Area of a rectangle = l x w • Rectangle is a generalization of a square. • To use the general function for a square • area (s,s) for a square.
What if area is more complicated? • What if the generalization does not adapt that easily? • Circles? • I.e. what if you have to use integration to calculate the area of the shape? • Generalize the function to allow you to define the calculation. dimension dimension function def area area
Another example • Sort routine to sort arbitrary objects • Sort algorithm is the same for all objects • Bubble sort, quick sort, etc • Comparison is dependent on the object Array of objects Comparison method
Conclusion • Using parameters allows for generalization and broader applicability of a function • Generalizing the functionality itself goes even further in widening the use if an operation is parameterized. • This is very much like using base classes in OOP to access more specific functionality in derived classes. • Standard example – area of figures
C++ example: function pointers… first step #include <iostream.h> //definition of 1 void func1(int a) { int r = a+2; cout << "Function 1 call result " << r << endl; } //definition of 2 void func2(int a) { int r = a*2; cout << "Function 2 call result " << r << endl; } void main(){ void (*fp)(int); // pointer to a void function // with a single integer param fp = func1; (*fp)(5); // call func1 fp = func2; (*fp)(5); // call func2 } outputs 7 outputs 10
C++ example: functions as parameters #include <iostream.h> void func1(int a) { int r = a+2; cout << "Function 1 call result " << r << endl; } void func2(int a) { int r = a*2; cout << "Function 2 call result " << r << endl; } void g( void (*f)(int), int x) { f(x); } void main(){ g( func1, 5); g( func2, 5); } outputs 7 outputs 10
Other more common uses • Windows programs use them for event handlers. • Define method/handler • Pass method to Operating System • Let OS call your method instead of you calling it • Threads • You define the run() method. • The OS will run this function as a thread using your run() function to start it • Called CALLBACKS. • Like giving someone a phone number to use to call you back.
Threads c++: another example void main() { _beginthread(( (void(*) void()) count, 0 , (void*) 5); count(4); } void count(int i) {int j; for (j=1; j<=i; j++) cout << j<<endl; } 1 2 1 2 3 3 4 4 1 2 1 2 3 3 4 4
Variations on the theme • Window GUI handlers do actually get a method to use in calling • C++ threads really do pass function pointers to the OS to use in invoking threads • Nasty! • Java does NOT actually pass a method name to the OS, rather uses a standard name. • Get objects that adhere to a “interface”
So how does java do it? • Everything already is a reference • Define an interface specification • Create a class abiding by that interface • Pass a reference to an object conforming to that interface • Execute that reference. • Does NOT allow passing function references