SlideServe
Menu
Préférences
© SlideServe · v2026

Functions as parameters general issue c++ java

hayden avatar
11 diapositives · 230 Views · Sep 01, 2012

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

Functions as parameters general issue c++ java - slide 1
01 / 11Functions as parameters general issue c++ java
1 / 17

Télécharger la présentation

Functions as parameters general issue c++ java
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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