Link to home
Start Free TrialLog in
Avatar of jskfan
jskfanFlag for Cyprus

asked on

Powershell Function

Powershell Function

I am running the code shown below. I enter a value and it should return the Cubicle value of it.
However I do not understand where the  outcome came from.

for instance, when I enter number 3 I should see 27,  instead of the outcome shown below

Thank you

User generated image
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Read-Host will treat the input as String, so you need to set it as Integer instead.

so try this instead

[int]$y=Read-Host("enter value: ")

Open in new window

Function get-cube([int]$x)

Open in new window

Avatar of Aard Vark
Aard Vark
Flag of Australia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Should you wonder what PowerShell did read: It saw
   "3" * "3" * "3"
and since there is no "string * string" operator but a "string * integer", it converts the second string into a number. "string * integer" means "repeat the string n times". So the expression is
  ("3" * 3) * "3"   ==>   "333" * "3"  ==>  "333" * 3   ==>   "333333333"
Write-Host ($result )   ->  You cannot access the value of $result this way since $result is local to that function. It is not available externally. so $result is $null here (ie out of the function) it is displaying $null value so you are not seeing any thing on display.

The output is from this -> Get-cube($y)
Avatar of jskfan


Thank you Guys!

will get to this topic in the future