Callback function with input arguments

(? Note that many functions which accept a callback as an argument usually require that the callback accept certain arguments.

The inner function has access to the variables and parameters of the outer function.))

Next is an simple example of the use of a callback in Python. First define two functions, the callback and the calling code, then pass the callback function into the calling code. The callback function has access to the variables and parameters of the calling function.

def my_callback(num):
    print("function my_callback was called with ", num)

def caller(val, cb):
    cb(val)

caller(5, my_callback)
caller(10, my_callback)

# run...
function my_callback was called with 5
function my_callback was called with 10

Function caller is invoked with a variable(val= 5) and a callback function my_callback, my_callback accesses the val and use integer 5 as it's input argument.

See another example

# callback
def double(x):
    print(x,' double is ', x*2)
def triple(x):
    print(x, ' triple is ', x*3)

def someAction(i,cb):
    cb(i)

someAction(5,double)     # 5 double is 10

someAction(3,triple)     # 3 triple is 9

In this example, a function someAction is defined with a parameter i intended for callback: cb. Another function double is defined that can be used as a callback to someAction. Other functions may be used for callback, like triple. In this example, someAction() is invoked twice, once with double as a callback and once with triple. The functions print out the double and triple, respectively.

An exercise: define a calling function doMath(x,y,cb), in it, print out x,y, and then run the callback function cb(x,y); define two callback functions for doMath: add(x,y) and multiply(x,y), they print out the sum or the product of x,y respectively.

def add(x,y):
    print('sum is ', x+y)
def multiply(x,y):
    print('product is ', x*y)    

def doMath(x,y,cb):
    print(x,y)
    cb(x,y)

doMath(2,4,add)         # 2,4 sum is 6
doMath(3,4,multiply)    # 3,4 product is 12

Function doMath is the caller, add and multiply are used as callback functions. When doMath is invoked, it also provided input arguments for it's callbacks.

Example in CodeCraft:

def oneRed(i):
    block(i, 1, -20, 'linen_red')
def twoBlue(i):
    block(i, 1, -20, 'linen_blue')
    block(i, 3, -20, 'linen_blue')

def build(n, cb):
    cb(n)

build(1, oneRed)
build(2, twoBlue)
build(5, oneRed)
build(7, twoBlue)

Using Callback in Strategy Design Pattern

In these primitive examples, the use of a callback is primarily a demonstration of principle. One could simply call the callbacks as regular functions, double(x), triple(s), add(x,y) or multiply(x,y).

How are they different from calling one function from another function taking some context from the calling function?

It is true that you are calling a function from another function, but the key is that the callback is treated like an Object, so you can change which Function to call based on the state of the system ??(like the Strategy Design Pattern).

(?? is it good to explain the above strategy design)

Depending on the provided number, if it's even, calculate it's double; if it's odd, calculated it's triple:

def double(x):
    print( 'double is ', x*2)
def triple(x):
    print('triple is ', x*3)
def showAction(a,cb):
    print(a)
    cb(a)

x = int(input('Enter an integer '))
if x%2 == 0:
    showAction(x,double)   
else:
    showAction(x,triple)
def single(n):
  for j in range(0, n):
    block(n, j*2+1, -20, 'box_green')

def double(n):
  for j in range(0,n):
    for k in [1,2]:
      block(n, k+3*j, -20, 'box_pink')

def action(n, cb):
    cb(n)

# pattern: even number, show double blocks; odd number, show single blocks
n = int(input('a number: '))
if n%2 == 0:
    print('even, double')
    action(n, double)
else:
    print('odd, single')
    action(n, single)

Callbacks are generally useful when the function needs to perform actions before the callback is executed.

((, or when the function does not (or cannot) have meaningful return values to act on, as is the case for Asynchronous JavaScript (based on timers))

results matching ""

    No results matching ""