![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Help with Functions and Variables Sub ConvertTemp() temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function I read this posting on Microsoft's website explaining the Function procedure. I do not see the point of using the fDegrees argument. I thought an argument passes a variable to the fucntion; yes, no? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Help with Functions and Variables "indytoatl" <indytoatl@xxxxxx> wrote in message news:dc94aafc-8f7f-4d0a-9279-c7ac744e54be@xxxxxx Quote: > Sub ConvertTemp() > temp = InputBox("Please enter the temperature in degrees F.", 1) > MsgBox "The temperature is " & Celsius(temp) & " degrees C." > End Sub > > Function Celsius(fDegrees) > Celsius = (fDegrees - 32) * 5 / 9 > End Function > > I read this posting on Microsoft's website explaining the Function > procedure. I do not see the point of using > the fDegrees argument. I thought an argument passes a variable to the > fucntion; yes, no? Sub ConvertTemp() temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub Sub ProcessTemp2() iInlet = 55 iOutlet = 70 MsgBox "The inlet temperature is " & Celsius(iInlet) & " degrees C." MsgBox "The outlet temperature is " & Celsius(iOutlet) & " degrees C." End Sub Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Help with Functions and Variables How do you reuse the function return value without re-running the function procedure? Wscript.echo "Results = " & Celsius & "something equals = " & Celsius Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Help with Functions and Variables > How do you reuse the function return value without re-running the Quote: > function procedure? > CelsVal = Celsius(32) Your example is "condensed", which makes it potentially confusing. It could have been: CelsVal = Celsius(temp) MsgBox "The temperature is " & CelsVal & " degrees C." In your example fDegrees is just the name of the parameter (also sometimes called an argument). You can have as many parameters as you want. (The name is just descriptive.) A parameter can also return information if desired. And you can often use a sub instead if you prefer: Temp = 32 Celsius Temp Msgbox Temp ' returns 0 Sub Celsius(fDegrees) fDegrees = (fDegrees - 32) * 5 / 9 End Sub Quote: > Wscript.echo "Results = " & Celsius & "something equals = " & Celsius > > Function Celsius(fDegrees) > Celsius = (fDegrees - 32) * 5 / 9 > End Function |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Help with Functions and Variables Hey, thanks for the info. Now how do I use the returned function value without kicking off the function again? Thanks in advance! ' ************* Begin Script ********************* GetDrivePath Wscript.echo GetDrivePath ' The statement above causes the Function to run again which is not ' what I'm trying to do. I just want to echo the path. Function GetDrivePath Set objFSO = CreateObject("Scripting.FileSystemObject") GetDrivePath = objFSO.GetFolder("C:\temp") End Fucntion |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Help with Functions and Variables > Hey, thanks for the info. Quote: > Now how do I use the returned function value without kicking off the > function again? > Thanks in advance! > to point out that a return variable always goes with a function. In the samples you're posting the code is condensed and the return variable is only implied. But it's still there. Whoever wrote the code knew that they could get away with compressing the whole thing into one line. For clarity it's maybe better to make the return variable explicit: '-- return result of GetDrivePath function in sPath variable: sPath = GetDrivePath() msgbox sPath A function has a variable to the left of the = sign. When it returns that variable is the result of the function. The function returns a value. A sub is the same thing; it just doesn't return a value. Quote: > ' ************* Begin Script ********************* > GetDrivePath > Wscript.echo GetDrivePath > > ' The statement above causes the Function to run again which is not > ' what I'm trying to do. I just want to echo the path. > > Function GetDrivePath > Set objFSO = CreateObject("Scripting.FileSystemObject") > GetDrivePath = objFSO.GetFolder("C:\temp") > End Fucntion > > |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Help with Functions and Variables On Sep 25, 9:23*am, indytoatl <indyto...@xxxxxx> wrote: Quote: > Hey, thanks for the info. > Now how do I use the returned function value without kicking off the > function again? > Thanks in advance! > > ' ************* Begin Script ********************* > GetDrivePath > Wscript.echo GetDrivePath > > *' The statement above causes the Function to run again which is not > ' *what I'm trying to do. I just want to echo the path. > > Function GetDrivePath > Set objFSO = CreateObject("Scripting.FileSystemObject") > GetDrivePath = objFSO.GetFolder("C:\temp") > End Fucntion result in a variable and then use the variable instead of the function call. sFolderPath = GetDrivePath Wscript.echo sFolderPath Tha particular example you are using is actually a bad one, since the GetFolder function that is invoked in your GetDrivePath function is not being used in a 'best practice' way. Rather, it is returning the path string only because it is the default property of the Folder object that is actually returned. Rather, I would use it thus ... set oFolder = GetDrivePath Wscript.echo oFolder.Path Function GetDrivePath Set objFSO = CreateObject("Scripting.FileSystemObject") set GetDrivePath = objFSO.GetFolder("C:\temp") End Function Then you can actually do what I think you are after - that is do something with the files contained in the temp folder named in the function, as in ... for each ofile in oFolder.Files wsh.echo oFile.name next Finally, do you have a copy of the WSH help docs - and maybe the Technet Script Center's sample scripts? Check these links out ... WSH 5.6 documentation download (URL all one line) http://www.microsoft.com/downloads/d...displaylang=en TechNet Script Center Sample Scripts (URL all one line) http://www.microsoft.com/downloads/d...a-b8814fe2da5a Tom Lavedas =========== http://members.cox.net/tglbatch/wsh/ |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Help with Functions and Variables Thanks guys!!! It makes sense now. WOW ;-) |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Functions and variables | PowerShell | |||
| math with "GB/MB/KB" in variables fails, without variables works? | PowerShell | |||
| $Variables | PowerShell | |||