What is a callback function?

■ What is a callback function?

A function that is passed as an argument to another function.

The C language example on the following site is easy to understand. A callback function is assigned to the function pointer and passed as an argument.

http://shiroibanana.blogspot.jp/2012/09/callback.html

#include<stdio.h>
typedef void (* FUNC_POINTER)(char *); 

//Cobuck function 1
void callback1(char *s){
    printf("%s\n",s);
}

//Callback function 2
void callback2(char *s){
    printf("[");
    printf("%s",s);
    printf("].\n");
}

//A function that calls a callback function
void func(char *s,FUNC_POINTER p){
    //Call the callback function
    p(s);
}

int main(){
    FUNC_POINTER p;

    p = callback1;
    //Call callback function 1
    func("Cobuck function 1",p);

    p = callback2;
    //Call callback function 2
    func("Cobuck function 2",p);
    return 0;
}

Execution result.

Cobuck function 1
[Cobuck function 2].

■ Python example

An example of using a callback function in python. It seems that the function that calls the callback function is often described as handler ↓

def handler(func,*args):
    return func(*args)

http://www.phactory.jp/cms/blog/post/admin/2010/02/python_funcpointer/

As a flow,

  1. Define a handler
  2. Define a callback function
  3. Assign a callback function to a function pointer
  4. Pass a function pointer as a handler argument
# -*- coding: utf-8 -*-
def handler(func,*args):
    return func(*args)

def callback1(message1):
    print("%s" % message1)

def callback2(message1, message2):
    print("%s, %s" % ( message1, message2) )

# main
if __name__ == "__main__":

   #Callback function assignment
   call = callback1
   #Handler execution:Callback function call
   handler(call, "message1")

   #Callback function assignment
   call = callback2
   #Handler execution:Callback function call
   handler(call, "message1", "message2")

Execution result.

message1
message1, message2

■ Use class

The member "func" in the class has a variable that acts as a function pointer, but basically the same thing is done.

  1. Define a class that acts as a handler
  2. Define a member to be treated as a function pointer
  3. Define a method that acts as a handler
  4. Define a callback function
  5. Assign a callback function to a function pointer
  6. Pass a function pointer as a handler argument
# -*- coding: utf-8 -*-
class Callbacker():
    def __init__(self, func):
        self.func = func

    def handler(self, *args):
        self.func(*args)

def callback1(message1):
    print '%s' % message1

def callback2(message1, message2):
    print("%s, %s" % ( message1, message2) )

# main
if __name__ == "__main__":

    #Callback function assignment
    callback = Callbacker(callback1)
    print (type(callback.func))
    print ("callback.func "),
    print (callback.func)
    print ("callback1     "),
    print (callback1)
    #Handler execution:Callback function call
    callback.handler("message1")
    print ("")

    #Callback function assignment
    callback.func = callback2
    print (type(callback.func))
    print ("callback.func "),
    print (callback.func)
    print ("callback2     "),
    print (callback2)
    #Handler execution:Callback function call
    callback.handler("message1", "message2")

Execution result

<type 'function'>
callback.func  <function callback1 at 0x7fcff9c8a9b0>
callback1      <function callback1 at 0x7fcff9c8a9b0>
message1

<type 'function'>
callback.func  <function callback2 at 0x7fcff9c8ab18>
callback2      <function callback2 at 0x7fcff9c8ab18>
message1, message2

■ Summary

A callback function is a function that is passed as an argument to another function.

Basically

  1. Define a handler
  2. Define a callback function
  3. Assign a callback function to a function pointer
  4. Pass a function pointer as a handler argument

Recommended Posts

What is a callback function?
What is the Callback function?
[Python] What is a zip function?
What is a distribution?
What is a terminal?
What is a hacker?
What is a pointer?
What is the activation function?
What is a decision tree?
What is a Context Switch?
What is a super user?
What is a system call
[Definition] What is a framework?
What is a python map?
[Python] What is a with statement?
What is a lexical scope / dynamic scope?
What is a Convolutional Neural Network?
What is namespace
What is copy.copy ()
What is dotenv?
What is Linux
What is klass?
What is SALOME?
What is a dog? Django installation volume
What is a dog? Python installation volume
What is Linux?
What is Linux
What is pyvenv
What is __call__
What is Linux
What is Python
What is a dog? Django--Create a custom user model
What is a dog? Challenge Django templates! Volume
It's a Mac. What is the Linux command Linux?
What is a dog? Django--Create a custom user model 2
Tell me what a conformal map is, Python!
What is Piotroski's F-Score?
What is Raspberry Pi?
[Python] What is Pipeline ...
What is Calmar Ratio?
[PyTorch Tutorial ①] What is PyTorch?
Differentiate a two-variable function
Make a function decorator
What is JSON? .. [Note]
What is Linux for?
What is ensemble learning?
What is TCP / IP?
What is Python's __init__.py?
What is UNIT-V Linux?
[Python] What is virtualenv
What is machine learning?
What is a dog? POST Sending Volume Using Django--forms.py
What is a dog? Django App Creation Start Volume--startapp
What is a dog? Django App Creation Start Volume--startproject
Basics of Python learning ~ What is a string literal? ~
What is a recommend engine? Summary of the types
What is God? Make a simple chatbot with python
[Python] Make sure the received function is a user-defined function
To myself as a Django beginner (2) --What is MTV?
What does the last () in a function mean in Python?
What is the difference between a symbolic link and a hard link?