how to pass argument as 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. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.
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.
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.
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);
Make argument as delegate, and call function with address of function which should match with delegates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With