views:

223

answers:

4

I have a function 'foo' and a variable '$foo' referencing it.

function foo
{
    param($value)
    $value + 5
}

$foo = foo
$foo

I can call $foo without args, but how can I pass parameters? This does not work:

$foo 5
$foo(5)

Actually the goal is to write such code:

function bar {
  param($callback)
  $callback 5
}

bar(foo)
+1  A: 

EDIT Misread the question the first time

What you need to do is use the & operator

& $foo 5
JaredPar
Actually you did not misread it, I just updated the question to be more clear :-) Btw, your solution still does not work...
alex2k8
+6  A: 

The problem is when you do

$foo = foo

You put the result of the function in the variable $foo, not the function itself !

Try this :

$foo = get-content Function:\foo

And call it like this

& $foo 5

Hope it helps !

Cédric Rup
A: 

$foo = foo 5

Shay Levy
+2  A: 

Use a script block.

# f.ps1

$foo = 
{
    param($value)
    $value + 5
}

function bar 
{
  param($callback)
  & $callback 5
}

bar($foo)

Running it:

> ./f.ps1
10
dangph