Avatar of jskfan
jskfan
Flag 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

f
PowershellScripting Languages

Avatar of undefined
Last Comment
jskfan

8/22/2022 - Mon
Ryan Chong

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

David Johnson, CD

or
Function get-cube([int]$x)

Open in new window

ASKER CERTIFIED SOLUTION
Aard Vark

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Qlemo

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"
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Raheman M. Abdul

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)
jskfan

ASKER
Thank you Guys!

will get to this topic in the future