Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Help with Functions and Variables

Reply
 
Old 09-24-2008   #1 (permalink)
indytoatl


 
 

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 SpecsSystem Spec
Old 09-24-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

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?
Perhaps this expanded example will make it clearer:

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 SpecsSystem Spec
Old 09-24-2008   #3 (permalink)
indytoatl


 
 

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 SpecsSystem Spec
Old 09-24-2008   #4 (permalink)
mayayana


 
 

Re: Help with Functions and Variables

> How do you reuse the function return value without re-running the
Quote:

> function procedure?
>
Usually you use a new variable:

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 SpecsSystem Spec
Old 09-25-2008   #5 (permalink)
indytoatl


 
 

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 SpecsSystem Spec
Old 09-25-2008   #6 (permalink)
mayayana


 
 

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!
>
That's what the variable is for. I was trying
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 SpecsSystem Spec
Old 09-25-2008   #7 (permalink)
Tom Lavedas


 
 

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
You need to apply what the mayayana told you, that is, store the
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 SpecsSystem Spec
Old 09-25-2008   #8 (permalink)
indytoatl


 
 

Re: Help with Functions and Variables

Thanks guys!!! It makes sense now. WOW ;-)

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Functions and variables PowerShell
math with "GB/MB/KB" in variables fails, without variables works? PowerShell
$Variables PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46