メモ
Docs の Call operator & と CallByName function を参照してください。
参考資料リンク
使い捨てスクリプト
ショートカット(.lnk)のリンク先(T):
PowerShell.exe -ExecutionPolicy RemoteSigned -Command "&{PowerShell_ISE.exe}#"
ショートカット(.lnk)の作業フォルダー(S):
(空欄)
Call operator &
# PowerShell 5.1, Windows 10 Set-StrictMode -Version Latest function f0([ref]$p) { Write-Host ( '関数f0: $($p.GetType().FullName)' ` + " #=> " ` + "$($p.GetType().FullName)" ` ) Write-Host ( '関数f0: $($p.Value.GetType().FullName)`n' ` + " #=> " ` + "$($p.Value.GetType().FullName)" ` ) Write-Host ( '関数f0: $($p.Value[0].GetType().FullName)`n' ` + " #=> " ` + "$($p.Value[0].GetType().FullName)" ` ) Write-Host ('関数f0: $p' + " #=> " + "$($p.Value)") $p.Value[0] /= 3 $p.Value[1] /= 4 Write-Host ('関数f0: $p' + " #=> " + "$($p.Value)") return $p.Value } function f1([ref]$q) { Write-Host ( '関数f1: $($q.GetType().FullName)' ` + " #=> " ` + "$($q.GetType().FullName)" ` ) Write-Host ( '関数f1: $($q.Value.GetType().FullName)' ` + " #=> " ` + "$($q.Value.GetType().FullName)" ` ) Write-Host ( ` '関数f1: $($q.Value[0].GetType().FullName)' ` + " #=> " ` + "$($q.Value[0].GetType().FullName)" ` ) Write-Host ('関数f1: $q' + " #=> " + "$($q.Value)") $q.Value[0] /= 5 $q.Value[1] /= 6 Write-Host ('関数f1: $q' + " #=> " + "$($q.Value)") return $q.Value } # /* ------------------------------ */ $f = @("f0", "f1") $a = 3 $b = 12 $x0 = @($a, $b) # /* Call operator & */ # 関数"f0" Write-Output ('実行前$x0' + " #=> " + "$x0") $x1 = &$f[0] -p ([ref]$x0) Write-Output ('実行後$x0' + " #=> " + "$x0") Write-Output ('実行後$x1' + " #=> " + "$x1") # /* Call operator & */ # 関数"f1" Write-Output ('実行前$x1' + " #=> " + "$x1") $x2 = &$f[1] -q ([ref]$x1) Write-Output ('実行後$x1' + " #=> " + "$x1") Write-Output ('実行後$x2' + " #=> " + "$x2") # /* ------------------------------ */ function Func([string]$f, [ref]$x) { # /* Call operator & */ return &$f $x } # /* ------------------------------ */ $c = 0.3 $d = 1.2 $y0 = @($c, $d) # 関数"Func"に文字列"f0" Write-Output ('実行前$y0' + " #=> " + "$y0") $y1 = Func -f $f[0] -x ([ref]$y0) Write-Output ('実行後$y0' + " #=> " + "$y0") Write-Output ('実行後$y1' + " #=> " + "$y1") # 関数"Func"に文字列"f1" Write-Output ('実行前$y1' + " #=> " + "$y1") $y2 = Func $f[1] ([ref]$y1) Write-Output ('実行後$y1' + " #=> " + "$y1") Write-Output ('実行後$y2' + " #=> " + "$y2")
CallByName function
' Excel 2019, Windows 10 ' Sheet1やThisWorkbookなどに記入してください Option Explicit Public Function f0(ByRef p() As Double) As Double() p(0) = p(0) / 3 p(1) = p(1) / 4 f0 = p End Function Public Function f1(ByRef q() As Double) As Double() q(0) = q(0) / 5 q(1) = q(1) / 6 f1 = q End Function Public Function Func(ByVal f As String, ByRef x() As Double) As Double() Func = VBA.Interaction.CallByName(Me, f, VBA.VbCallType.VbMethod, x) End Function Public Sub Main() Dim f(0 To 1) As String f(0) = "f0" f(1) = "f1" Dim a As Double, b As Double a = 3 b = 12 Dim x0(0 To 1) As Double x0(0) = a x0(1) = b Debug.Print "x0" & " #=> " & x0(0) & " " & x0(1) Dim x1() As Double x1 = VBA.Interaction.CallByName(Me, f(0), VBA.VbCallType.VbMethod, x0) Debug.Print "x0" & " #=> " & x0(0) & " " & x0(1) Debug.Print "x1" & " #=> " & x1(0) & " " & x1(1) Dim x2() As Double x2 = VBA.Interaction.CallByName(Me, f(1), VBA.VbCallType.VbMethod, x1) Debug.Print "x1" & " #=> " & x1(0) & " " & x1(1) Debug.Print "x2" & " #=> " & x2(0) & " " & x2(1) Dim c As Double, d As Double c = 0.3 d = 1.2 Dim y0(0 To 1) As Double y0(0) = c y0(1) = d Debug.Print "y0" & " #=> " & y0(0) & " " & y0(1) Dim y1() As Double y1 = Func(f:=f(0), x:=y0) Debug.Print "y0" & " #=> " & y0(0) & " " & y0(1) Debug.Print "y1" & " #=> " & y1(0) & " " & y1(1) Dim y2() As Double y2 = Func(f(1), y1) Debug.Print "y1" & " #=> " & y1(0) & " " & y1(1) Debug.Print "y2" & " #=> " & y2(0) & " " & y2(1) End Sub