// Visual-C Win32 Konsole Project VC++.NET 2003

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// See http://en.wikipedia.org/wiki/Callback_(computer_programming)
// Source http://www.gfai.de/~heinz/techdocs/index.htm


// The calling function takes a single callback *sourcefct as a parameter
void mycallbackfct(int (*sourcefct)(void)) {
	printf("\n call: (a) = %d;\t (b) = %d;\t (c) = %d;\n", sourcefct(), sourcefct(), sourcefct());
}

// a possible callback
int randomNumber(void) {
    return (rand() % 1000) + 10000;
}
 
// another possible callback
int mensHobby(void) {
	return (5+1);
}

// a third callback
int getTime(void) {
	time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
	return(now->tm_sec);
}

// further callback
int secsSince1970(void) {
	double seconds = time(NULL);	// 1970
    return (seconds);		// secs since 1970
}

// to find the direction of calls
int heinzels(void) {
	static int i;
	i++;
	return (i);
}



int main(void) {

	printf("\n\nDifferent callback-functions:\n");

	// we call mycallbackfct() with different functions
	mycallbackfct(&rand);
    mycallbackfct(&mensHobby);
    mycallbackfct(&randomNumber);
	mycallbackfct(&getTime);
	mycallbackfct(&secsSince1970);

	printf("\n\nFind the direction of calls:\n");
	mycallbackfct(&heinzels);

	printf("\n\nWe find the calls in printf() from right to left!\n\n");

	getchar();
    return 0;
}



/******** Result ***********************************



Different callback-functions:
 call: (a) = 6334;       (b) = 18467;    (c) = 41;
 call: (a) = 6;  (b) = 6;        (c) = 6;
 call: (a) = 10724;      (b) = 10169;    (c) = 10500;
 call: (a) = 15;         (b) = 15;       (c) = 15;
 call: (a) = 1160156095;         (b) = 1160156095;       (c) = 1160156094;

Find the direction of calls:
 call: (a) = 3;  (b) = 2;        (c) = 1;

We find the calls in printf() from right to left!

*/