Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an argument to a function pointer parameter?

I only just started learning Python and found out that I can pass a function as the parameter of another function. Now if I call foo(bar()) it will not pass as a function pointer but the return value of the used function. Calling foo(bar) will pass the function, but this way I am not able to pass any additional arguments. What if I want to pass a function pointer that calls bar(42)?

I want the ability to repeat a function regardless of what arguments I have passed to it.

def repeat(function, times):     for calls in range(times):         function()  def foo(s):         print s  repeat(foo("test"), 4) 

In this case the function foo("test") is supposed to be called 4 times in a row. Is there a way to accomplish this without having to pass "test" to repeat instead of foo?

like image 259
Byzantian Avatar asked Dec 08 '12 23:12

Byzantian


People also ask

How do you pass a parameter to a function pointer?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

Can we pass formal parameter as a pointer to function?

If you declare a formal parameter of a function as a pointer type, you are passing that parameter by its address. The pointer is copied, but not the data it points to. So, Pass By Address offers another method of allowing us to change the original argument of a function (like with Pass By Reference).

How do you pass a pointer to a value in C++?

To pass the value by pointer, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.

How do you use a pointer to a function?

You can use a trailing return type in the declaration or definition of a pointer to a function. For example: auto(*fp)()->int; In this example, fp is a pointer to a function that returns int .

Can you pass a pointer as an argument to a function?

Just like any other argument, pointers can also be passed to a function as an argument. Lets take an example to understand how this is done. In this example, we are passing a pointer to a function. When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value.

How do you pass a function as a parameter in C?

In C programming you can only pass variables as parameter to function. You cannot pass function to another function as parameter. But, you can pass function reference to another function using function pointers. Using function pointer you can store reference of a function and can pass it to another function as normal pointer variable.

How to pass a variable as an argument to a function?

Till now, we have seen that in C programming, we can pass the variables as an argument to a function. We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer.

Why do we use pointers in C programming?

If any change made by the function using pointers, then it will also reflect the changes at the address of the passed variable. Therefore, C programming allows you to create a pointer pointing to the function, which can be further passed as an argument to the function.


2 Answers

You can either use a lambda:

repeat(lambda: bar(42)) 

Or functools.partial:

from functools import partial repeat(partial(bar, 42)) 

Or pass the arguments separately:

def repeat(times, f, *args):     for _ in range(times):         f(*args) 

This final style is quite common in the standard library and major Python tools. *args denotes a variable number of arguments, so you can use this function as

repeat(4, foo, "test") 

or

def inquisition(weapon1, weapon2, weapon3):     print("Our weapons are {}, {} and {}".format(weapon1, weapon2, weapon3))  repeat(10, inquisition, "surprise", "fear", "ruthless efficiency") 

Note that I put the number of repetitions up front for convenience. It can't be the last argument if you want to use the *args construct.

(For completeness, you could add keyword arguments as well with **kwargs.)

like image 73
Fred Foo Avatar answered Sep 20 '22 13:09

Fred Foo


You will need to pass the parameters for foo, to the repeat function:

#! /usr/bin/python3.2  def repeat (function, params, times):     for calls in range (times):         function (*params)  def foo (a, b):     print ('{} are {}'.format (a, b) )  repeat (foo, ['roses', 'red'], 4) repeat (foo, ['violets', 'blue'], 4) 
like image 37
Hyperboreus Avatar answered Sep 17 '22 13:09

Hyperboreus