Problem using Powershell return in statement

function a() { return 10 } $b = 10 write-Host "b = $b" [int]$b += a write-Host "b = $b" [int]$b = a + 10 write-Host "b = $b" Output for the above script is: b = 10 b = 20 b = 10 

I have C programming and recently started programming in PS. I tried to write a simple program that uses the return value from a function in an instruction. I am confused by the results. The following statements work differently.

 [int]$b += a [int]$b = a + 10 

Can someone explain the reason to me?

Hi

Jugari

+4
source share
2 answers

Yes, strange for programmer S.

[int]$b = a + 10 means that you are calling a function with two parameters try [int]$b = (a) + 10

repeat this function

 function a() { write-host $args.count return 10 } 

You must also be very careful with the values ​​returned by the function.

+3
source

I suspect the interpreter is trying to pass "+ 10" as parameters to your function; because your function has no parameters, they are set to null. Putting parentheses around your function call will prevent this, and I suspect you are planning work.

 [int]$b = (a) + 10 write-Host "b = $b" 
0
source

Source: https://habr.com/ru/post/1500252/


All Articles