1 / 11

Functions as parameters general issue c++ java

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

hayden
Download Presentation

Functions as parameters general issue c++ java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions as parametersgeneral issuec++java

  2. 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.

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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.

  9. 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

  10. 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”

  11. 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

More Related