Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing function as argument in the function

Tags:

c#

how to pass argument as function

like image 588
subbiah Avatar asked Jun 25 '10 04:06

subbiah


People also ask

How do you pass a function as an argument?

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. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.

Can a function be passed as an argument to a function?

Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.

Can you pass a function as an argument to another function in Python?

Summary. In Python you can pass function objects in to other functions. Functions can be passed around in Python. In fact there are functions built into Python that expect functions to be given as one or more of their arguments so that they can then call them later.


2 Answers

You're probably looking for delegates.

public delegate void MyDelegate(int myInt, string myString);
public void FunctionToCall(int i, string s)
{
    Console.WriteLine(s + " [" + i.ToString() + "]");
}
public void MethodWithFunctionPointer(MyDelegate callback)
{
    callback(5, "The value is: ");
}

And then, to call it:

MethodWithFunctionPointer(FunctionToCall);
like image 183
drharris Avatar answered Sep 22 '22 09:09

drharris


Make argument as delegate, and call function with address of function which should match with delegates

like image 33
IBhadelia Avatar answered Sep 25 '22 09:09

IBhadelia