Charles Tritt
/
FunctionPointers
Demonstrates the use of function pointers.
main.cpp@0:f106e3c0c589, 2017-11-05 (annotated)
- Committer:
- CSTritt
- Date:
- Sun Nov 05 20:27:53 2017 +0000
- Revision:
- 0:f106e3c0c589
Initial version. Based on Horton example 9.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:f106e3c0c589 | 1 | /* |
CSTritt | 0:f106e3c0c589 | 2 | Project: FunctionPointer |
CSTritt | 0:f106e3c0c589 | 3 | File: main.cpp |
CSTritt | 0:f106e3c0c589 | 4 | Modified by: Dr. C. S. Tritt |
CSTritt | 0:f106e3c0c589 | 5 | Last revised: 11/5/17 (v. 1.0) |
CSTritt | 0:f106e3c0c589 | 6 | |
CSTritt | 0:f106e3c0c589 | 7 | Based on Horton Program 9.1 Pointing to functions. |
CSTritt | 0:f106e3c0c589 | 8 | */ |
CSTritt | 0:f106e3c0c589 | 9 | #include "mbed.h" |
CSTritt | 0:f106e3c0c589 | 10 | |
CSTritt | 0:f106e3c0c589 | 11 | // Function definitions (a.k.a., prototypes). |
CSTritt | 0:f106e3c0c589 | 12 | int sum(int, int); |
CSTritt | 0:f106e3c0c589 | 13 | int product(int, int); |
CSTritt | 0:f106e3c0c589 | 14 | int difference(int, int); |
CSTritt | 0:f106e3c0c589 | 15 | |
CSTritt | 0:f106e3c0c589 | 16 | int main(void) |
CSTritt | 0:f106e3c0c589 | 17 | { |
CSTritt | 0:f106e3c0c589 | 18 | int (*pfun)(int, int); // Local function pointer declaration. |
CSTritt | 0:f106e3c0c589 | 19 | int a = 10; // Initial value for a. |
CSTritt | 0:f106e3c0c589 | 20 | int b = 5; // Initial value for b. |
CSTritt | 0:f106e3c0c589 | 21 | |
CSTritt | 0:f106e3c0c589 | 22 | pfun = sum; // pfun now points to function sum(). |
CSTritt | 0:f106e3c0c589 | 23 | int result = pfun(a, b); // Call sum() through pointer. |
CSTritt | 0:f106e3c0c589 | 24 | printf("pfun = sum result = %d\n", result); |
CSTritt | 0:f106e3c0c589 | 25 | |
CSTritt | 0:f106e3c0c589 | 26 | pfun = product; // pfun now points to function product(). |
CSTritt | 0:f106e3c0c589 | 27 | result = pfun(a, b); // Call product() through pointer. |
CSTritt | 0:f106e3c0c589 | 28 | printf("pfun = product result = %d\n", result); |
CSTritt | 0:f106e3c0c589 | 29 | |
CSTritt | 0:f106e3c0c589 | 30 | pfun = difference; // pfun now points to function difference(). |
CSTritt | 0:f106e3c0c589 | 31 | result = pfun(a, b); // Call difference() through pointer. |
CSTritt | 0:f106e3c0c589 | 32 | printf("pfun = difference result = %2d\n", result); |
CSTritt | 0:f106e3c0c589 | 33 | |
CSTritt | 0:f106e3c0c589 | 34 | while (true) wait(3600.0f); // Wait forever, over and over. |
CSTritt | 0:f106e3c0c589 | 35 | } |
CSTritt | 0:f106e3c0c589 | 36 | |
CSTritt | 0:f106e3c0c589 | 37 | // Function declarations... |
CSTritt | 0:f106e3c0c589 | 38 | int sum(int x, int y) |
CSTritt | 0:f106e3c0c589 | 39 | { |
CSTritt | 0:f106e3c0c589 | 40 | return x + y; |
CSTritt | 0:f106e3c0c589 | 41 | } |
CSTritt | 0:f106e3c0c589 | 42 | |
CSTritt | 0:f106e3c0c589 | 43 | int product(int x, int y) |
CSTritt | 0:f106e3c0c589 | 44 | { |
CSTritt | 0:f106e3c0c589 | 45 | return x * y; |
CSTritt | 0:f106e3c0c589 | 46 | } |
CSTritt | 0:f106e3c0c589 | 47 | |
CSTritt | 0:f106e3c0c589 | 48 | int difference(int x, int y) |
CSTritt | 0:f106e3c0c589 | 49 | { |
CSTritt | 0:f106e3c0c589 | 50 | return x - y; |
CSTritt | 0:f106e3c0c589 | 51 | } |