language agnostic - What are callback methods? -


i'm programming noob , didn't quite understand concept behind callback methods. tried reading in wiki , went on head. can please explain in simple terms?

the callback pass function, tells should call @ point in operation. code in function decides when call function (and arguments pass). typically, way pass the function itself 'callback', in languages functions objects. in other languages, might have pass kind of special thing called "function pointer" (or similar); or might have pass name of function (which gets looked @ runtime).

a trivial example, in python:

void call_something_three_times(what_to_call, what_to_pass_it):     in xrange(3): what_to_call(what_to_pass_it)  # write "hi mom" 3 times, using callback. call_something_three_times(sys.stdout.write, "hi mom\n") 

this example let separate task of repeating function call, task of calling function. that's not useful, demonstrates concept.

in real world, callbacks used lot things threading libraries, call thread-creation function callback describes work thread do. thread-creation function necessary work set thread, , arranges callback function called new thread.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -