CS112_27_Slides-MappingFunction

# CS112_27_Slides-MappingFunction - Mapping Functions Yinglin...

This preview shows pages 1–5. Sign up to view the full content.

CS112 Fundamentals of Programming Abstractions Mapping Functions Yinglin Wang CS Dept., SJTU CS112 Fundamentals of Programming Abstractions Review Pointers to functions can be interpreted as data values in C

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112 Fundamentals of Programming Abstractions The printTable function void PrintTable(int start, int end, IntFnTfn) { int x, y; for (x = start; x <= end; x++) { y = fn(x); printf("%d %d\n", x, y); } } int Square(int n) { return n * n; } int Cube(int n) { return n * n * n; } PrintTable(1, 5, Square); PrintTable(1, 5, Cube); Call back functions typedef int (*IntFnT)(int); CS112 Fundamentals of Programming Abstractions Diagram of the calls printTable(1,5,Cube); printTable 1 5 Cube Cube 1 5 typedef int (*IntFnT)(int);
CS112 Fundamentals of Programming Abstractions Callback functions Functions that are passed by clients to an implementation, such as the various comparison functions(e.g., squre, cube in the last slide. IntCmpFn, StringCmpFn in the next slide), are called callback functions . Callback functions make it possible for the client to pass information to the implementation about how a particular data behaves.the usual approach is for the client to supply a callback function that can then be invoked on the implementation side callback functions play an important role in modern programming methodology and are an integral part of the implementation of object-oriented languages. CS112 Fundamentals of Programming Abstractions The qsort function void qsort(void* array, int elementsNum, intelementSize, CmpFnT cmpFn) { some sorting code appeared here. … … p1=(void *)((char *)array+k*elementSize); p2=… call IntCmpFn(p1, p2) … .. } typedefint (*CmpFnT)(void* p1, void * p2) int IntCmpFn(void*p1, void *p2) { int v1, v2; v1 = *((int *) p1); v2 = *((int *) p2); if (v1 == v2) return 0; ... } qsort(array, n, sizeof(int), IntCmpFn) array Call back functions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112 Fundamentals of Programming Abstractions If we provide a string compare function, then qsort could sort an array of strings. It would work just as it does for ints, passing the addresses of two elements to the compare function. The elements are strings, so the compare function gets string *'s. Note that the arguments are pointers to strings and not pointers to characters! That means that the function StringCompare isn’ t quite the right function-- we need a wrapper. CS112 Fundamentals of
This is the end of the preview. Sign up to access the rest of the document.

### Page1 / 16

CS112_27_Slides-MappingFunction - Mapping Functions Yinglin...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online