Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions as parameters general issue c++ java. What? Typically programmers use data structures as the means to give specific definition to a general.

Similar presentations


Presentation on theme: "Functions as parameters general issue c++ java. What? Typically programmers use data structures as the means to give specific definition to a general."— Presentation transcript:

1 Functions as parameters general issue c++ 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 = s 2 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 area dimension area function def

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 //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 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 count(int i) {int j; for (j=1; j<=i; j++) cout << j<<endl; } 1212334412123344 void main() { _beginthread(( (void(*) void()) count, 0, (void*) 5); count(4); } 1212334412123344

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


Download ppt "Functions as parameters general issue c++ java. What? Typically programmers use data structures as the means to give specific definition to a general."

Similar presentations


Ads by Google