Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass function as a parameter

I've written function 'A' that will call one of a number of other functions. To save re-writing function 'A', I'd like to pass the function to be called as a parameter of function 'A'. For example:

function A{     Param($functionToCall)     Write-Host "I'm calling : $functionToCall" }  function B{     Write-Host "Function B" }  Function C{     write-host "Function C" }  A -functionToCall C 

Returns: I'm calling: C

I am expecting it to return: I'm calling: Function C.

I've tried various things such as:

Param([scriptblock]$functionToCall) 

Cannot convert System.String to ScriptBlock

A -functionToCall $function:C 

Returns "Write-Host "Function C"

A - functionToCall (&C) 

This evaluates before the rest of it:

 Function C  I'm Calling : 

I'm sure this is programming 101, but I can't work out the correct syntax or what it is I'm doing wrong.

like image 406
woter324 Avatar asked Mar 02 '14 15:03

woter324


People also ask

Can we pass function as parameter?

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. In the example below, a function greet is created which takes a function as an argument.

Can I pass function as parameter in JavaScript?

Functions in the functional programming paradigm can be passed to other functions as parameters. These functions are called callbacks. Callback functions can be passed as arguments by directly passing the function's name and not involving them.

Can you pass functions as parameters in C?

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.

Can I pass a function as a parameter in Python?

Yes it is, just use the name of the method, as you have written. Methods and functions are objects in Python, just like anything else, and you can pass them around the way you do variables. In fact, you can think about a method (or function) as a variable whose value is the actual callable code object.


2 Answers

I'm not sure this is the best, but:

function A{     Param([scriptblock]$FunctionToCall)     Write-Host "I'm calling $($FunctionToCall.Invoke(4))" }  function B($x){     Write-Output "Function B with $x" }  Function C{     Param($x)     Write-Output "Function C with $x" }  PS C:\WINDOWS\system32> A -FunctionToCall $function:B I'm calling Function B with 4  PS C:\WINDOWS\system32> A -FunctionToCall $function:C I'm calling Function C with 4  PS C:\WINDOWS\system32> A -FunctionToCall { Param($x) "Got $x" } I'm calling Got x 
like image 144
Duncan Avatar answered Sep 19 '22 19:09

Duncan


Have you thought about passing a ScriptBlock as a parameter?

$scriptBlock = { Write-Host "This is a script block" } Function f([ScriptBlock]$s) {   Write-Host "Invoking ScriptBlock: "   $s.Invoke() }  PS C:\> f $scriptBlock Invoking ScriptBlock: This is a script block 
like image 38
Rustam Avatar answered Sep 18 '22 19:09

Rustam