-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8868474
commit ba56a48
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
// Store your function in a variable. | ||
// Function with no arguments and no return. | ||
void (*func)() = 0; | ||
// Function with two arguments and return. | ||
int (*arg_func)(int, int) = 0; | ||
|
||
void print() | ||
{ | ||
cout << "Hooray" << endl; | ||
} | ||
|
||
int sum(int a, int b) | ||
{ | ||
return a + b; | ||
} | ||
|
||
// Passing function as argument. | ||
void call_function(void (*f)()) | ||
{ | ||
f(); | ||
} | ||
|
||
// Passing function with parameters as argument. | ||
int call_arg_function(int (*f)(int, int)) | ||
{ | ||
return f(2, 2); | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
func = print; | ||
func(); // Print Hooray. | ||
call_function(print); // Print Hooray. | ||
|
||
arg_func = sum; | ||
cout << arg_func(2, 2) << endl; // Print 4. | ||
cout << call_arg_function(sum) << endl; // Print 4. | ||
|
||
return 0; | ||
} |