Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Another function question

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,45440
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Another function question
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Another function question - 4/3/2007 7:14:54 PM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
After learning a bit about functions, and managing to implement one into my code ok, there's 1 thing I don't fully understand:

My function "ExtraInfo" needs some info passed into it to process, held in variable strNewName.  So when I call my function, I use this code:

strExtraInfo = ExtraInfo(strNewName)

My understanding is strNewName in there in () to tell the code to pass that variable into the function.

But also the first line of my Function reads:

Function ExtraInfo(strNewName)

My question is it necessary to have strNewName in () at both points? If so, why?

Thanks a lot, I'm getting there!

 
 
Post #: 1
 
 RE: Another function question - 4/3/2007 7:42:39 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
strNewName in the Function or Sub and also be a different name. [as ginolard explained to me once]

e.g.
strNewName="Hello World"
ExtraInfo(strNewName)

Function ExtraInfo(FuzzyWuzzyBunny)
msgbox FuzzyWuzzyBunny
End Function

(in reply to markmcrobie)
 
 
Post #: 2
 
 RE: Another function question - 4/3/2007 7:56:13 PM   
  ginolard


Posts: 1020
Score: 21
Joined: 8/10/2005
Status: offline
Yeah, it's not really a good idea to reuse variable names like that.  Whilst it works, it's just confusing ;)

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to gdewrance)
 
 
Post #: 3
 
 RE: Another function question - 4/3/2007 8:02:36 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
yip, I know, at the time if you remember I was very confused. But I thought it would help someone on how Functions from other scripts are/could be reused where variable names are different

(in reply to ginolard)
 
 
Post #: 4
 
 RE: Another function question - 4/3/2007 8:52:26 PM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
So what's the difference between the variable in () when you call the Function, and the variable in () in the Function <name>() line when you start writing the function?

(in reply to markmcrobie)
 
 
Post #: 5
 
 RE: Another function question - 4/3/2007 9:02:36 PM   
  siggy

 

Posts: 10
Score: 0
Joined: 4/2/2007
Status: offline
Hi

The name used when declaring / writing the function is just a descriptive name that will be used in the code of the function.
It can be anything you like, as long as it's used in the code body of the function. 
It's just a name tag / replacement for the variable that's being passed into function from the 'outside' code (which is strNewName in your case).

The function and all it's varibables, names, etc are seperate to the main script code (or should be - no globals in good prog'ing, unless it's neccessary :o).  

Eg.  These functions are all the same & do the same thing

Function ExtraInfo(strIntoFunc)
strIntoFunc = strIntoFunc + "z"
ExtraInfo = strIntoFunc
end function

or

Function ExtraInfo(strIn)
strIn = strIn + "z"
ExtraInfo = strIn
end function

or

Function ExtraInfo(fred)
fred = fred + "z"
ExtraInfo = fred
end function


You'd use any of these func's (only one of the above would be defined in your script) in exactly the same way in your script code:

Dim str1, str2

str1 = "pete"
str2 = "paul"

WScript.Echo ExtraInfo(str1)
WScript.Echo ExtraInfo(str2)

So:-


      

< Message edited by siggy -- 4/3/2007 9:13:42 PM >

(in reply to markmcrobie)
 
 
Post #: 6
 
 RE: Another function question - 4/3/2007 9:03:20 PM   
  ginolard


Posts: 1020
Score: 21
Joined: 8/10/2005
Status: offline
There's no difference.  They are the same data just with different variable names.

Now, you might be asking why not just have one variable name for use throughout the entire script and then there'd be no need to have something like


      

You could just have

      

That's certainly possible but then you might want to go and read the "Dim in Subs" thread first ;)

< Message edited by ginolard -- 4/3/2007 9:04:30 PM >


_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to markmcrobie)
 
 
Post #: 7
 
 RE: Another function question - 4/3/2007 9:33:13 PM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Siggy I kinda understand what you said, but how does that code out a z on the end of str1 and str2?

(in reply to ginolard)
 
 
Post #: 8
 
 RE: Another function question - 4/3/2007 9:50:42 PM   
  ginolard


Posts: 1020
Score: 21
Joined: 8/10/2005
Status: offline
Ok, let's walk through it slowly (I feel like ehvbs already!)


      

This is our function called ExtraInfo.  It expects some input and calls it fred.  fred will become a variable WITHIN the function.  When the function is run it takes the variable fred and sticks a z on the end and then has a conversation with itself

"Hey ExtraInfo!"
"Yeah?"
"Whenever anyone asks you what you are, tell them are you Fred with a z on the end"
"OK"

So, that means when we do this

WScript.echo ExtraInfo("123") the conversation above happens and Extrainfo is equal to 123z

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to markmcrobie)
 
 
Post #: 9
 
 RE: Another function question - 4/3/2007 10:18:42 PM   
  siggy

 

Posts: 10
Score: 0
Joined: 4/2/2007
Status: offline
Okay here goes an explanation of how you might want to visualise how functions work.
It's not actually what happens, but it explains (I hope).

There are 2 ways to pass parameters to a function, one is to pass by value & the other (the default in vbscript )is pass by reference.
This code below shows them both working.  2 functions that do exactly the same thing, but each using the 2 different ways of passing the param's as mentioned above.


      

okay so we declare 2 var's str1 & str2, which are set to hold values 'Pete' & 'paul'.

First, I call a function ExtraInfoByVal which is passed str1 to work on.
Because this func is set up to pass-by-value, it takes str1 & creates a copy of the str1 data, and calls it 'fred'.
The function then does it's magic to this fred copy (ie, add a z to the end) and it returns the value back to the script, which echo's the value, ie. 'petez'.
You could store this new string value in another varibale if you wanted.
Because the func worked on a copy of the str1 data, str1 remains unchanged by the func, so when I echo it after using ExtraIntoByVal it remains unchanged, ie. It's still 'pete'.

Next, I call ExtrInfoByVal again, but this time pass it str2.
It creates a copy of str2, renames this copy fred & does it's magic on fred, then it returns fred's new value of 'paulz'.
str2 in the script remains  unchanged & is still 'paul' after running the function.

Next I call the func ExtraInfoByRef and pass it str1 (which is still holding the value 'pete' remember).
This time the value is passed-by-reference and so the function acts on the data str1 itself. 
It renames str1 to fred internally to itself, does it's magic on fred (ie. adds a z)
and returns the value of fred back to the outside script.  So echoing the return value of ExtraInfoByRef gives 'petez'.
However, this function acted directly on the data sent to it (& not a copy of the data, as it did before) so now if we echo str1 it gives us a value of 'petez'.

Finally, I call ExtrInfoByRef passing it str2.
The func renames str2 to fred (internally to itself), does it's magic to fred (ie. adds a z to the end of fred) & returns the value to the calling script (ie. returns 'paulz').
After the func is called str2 now hold the value 'paulz', because the func was acting directly on the data sent to it (and not a copy, as happened in the pass by value form of the function).

I hope this illustration helps you.

To really understand what's going on you'll need to undertand how data is actually held in memory & how pointers & variable names really work / point to memory.
But I wouldn't worry about this for now.

Siggy

(in reply to markmcrobie)
 
 
Post #: 10
 
 RE: Another function question - 4/3/2007 11:12:12 PM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
Yes, that helps, thanks, I'm getting there.

I think I understand the variable in brackets when we *call* the function (as I see it this is a variable that exists in the outside script that we want to pass into the function, either byVal or byRef, so the function can do something to it and return the result if need be).

What I'm not 100% clear on the variable name in brackets when we *declare* the function. 

(in reply to siggy)
 
 
Post #: 11
 
 RE: Another function question - 4/3/2007 11:46:30 PM   
  siggy

 

Posts: 10
Score: 0
Joined: 4/2/2007
Status: offline
The name used for the parameter in the brackets when the function is declared is just a name (any name at all) that we want to use / refer to inside the function body.

This name is usually one that best describes the data to the progrmamer for use in the function.

Think of this name, & the function, as being totally seperate to any code that might later use the function.

The function is a totally separate, autonomus piece of code which should be able to stand on it's own.
The names of func param's, var's it uses, etc should all be its own & be held within itself.

Maybe a better way to look at it is this, if you look at this function:

function myFunc(fred)
  fred = fred +"z"
  myFunc = fred
end function

It's a piece of code that works on it's own & is saying:
I'm a function, I'm going to be handed a piece of data - I don't know what the person who calls me will call this data in his program, but I'll call it 'fred'.
Once I get this data, which I call 'fred', I'm going to add a 'z' to the end of it.  Then I'll tell the code that called me what the data is now that I've added a 'z' to the end of it.

If the function had been declared:

function myFunc(byVal fred)
  fred = fred +"z"
  myFunc = fred
end function

The function would be saying to itself:
I'm a function, that's going to passed some data.  I'm not allowed to change this data, so I'll make a copy of this data.  I'll call my copy of the data 'fred', because I don't know what the programmer will be calling his data in his code.  Then I'll add a 'z' to the end of the copied data, which I've called 'fred'.  Then I'll let the programmer know what my copy of the data (ie. fred) now looks like.  His data will be the same as it was when he handed it to me, but my copy of the data will have a 'z' on the end.  Now that I've done my job, I'll delete my copy of the data called 'fred', 'coz I've finished my work for now.

< Message edited by siggy -- 4/4/2007 12:40:27 AM >

(in reply to markmcrobie)
 
 
Post #: 12
 
 RE: Another function question - 4/3/2007 11:48:44 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
Hope this helps whats going on in the parentheses

better = "wow"
one = "two"
DeleteOldFile better, one

Function DeleteOldFiles (sDirPath, iNumberofDays) 'sDirPath = better which is value of "wow"  iNumberofDays = one value of "two"
wscript.echo sDirpath
wscript.echo iNumberofDays
end Function


DeleteOldFile "better", 1

Function DeleteOldFile (sDirPath, iNumberofDays) 'string "better" = sDirPath and 1 = iNumberofDays
wscript.echo sDirpath
wscript.echo iNumberofDays
end Function

(in reply to markmcrobie)
 
 
Post #: 13
 
 RE: Another function question - 4/4/2007 1:38:30 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
If I understand the base question, it is "Why do you need to use () when you declare a function?". If that is the question, then the answer is because the people that developed vbscript say so. That is all it is simply a convention. They could have just as easily written the script engine so that you could do the same thing without the () they just didn't happen to do it that way.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to gdewrance)
 
 
Post #: 14
 
 RE: Another function question - 4/4/2007 6:34:51 AM   
  TNO


Posts: 1056
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
We discussed this topic in detail a few months ago:

http://www.visualbasicscript.com/m_40908/mpage_1/key_parenthesis/tm.htm#40958

_____________________________

Consolidated Script Component: The Acid Test

A universe of complexity...

(in reply to ebgreen)
 
 
Post #: 15
 
 RE: Another function question - 4/4/2007 6:57:33 PM   
  markmcrobie

 

Posts: 314
Score: 0
Joined: 12/12/2006
Status: offline
No, my question isn't about *why* we need parenthisis, it's about *what* we put between them, and why.

(in reply to TNO)
 
 
Post #: 16
 
 RE: Another function question - 4/4/2007 10:38:31 PM   
  alex87


Posts: 27
Score: 0
Joined: 3/20/2007
From: Netherland
Status: offline
testing if the forum works again

(in reply to markmcrobie)
 
 
Post #: 17
 
 RE: Another function question - 4/4/2007 11:10:45 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
never knew it was broken

(in reply to alex87)
 
 
Post #: 18
 
 RE: Another function question - 4/5/2007 1:38:30 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
quote:

No, my question isn't about *why* we need parenthisis, it's about *what* we put between them, and why.


So have you gotten an adequate answer?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to gdewrance)
 
 
Post #: 19
 
 RE: Another function question - 4/26/2007 2:35:17 PM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
Okie, I looked through the fora for functions, theres 300 links.   =(
Can someone tell me if you could pass multiple returned variables from a function?

I.E.

Call ExtraInfo(strTest)
WScript.Echo ExtraInfo(strTest) & "  " & strTest & "  " & strComputer

Function ExtraInfo(strTest)
strTest = "Funcion Testing"
ExtraInfo = "Extra info Testing"
StrComputer = "CompName Testing"
End Function


I could echo the strTest and ExtraInfo, but how would I pass the variable strComputer outside of the function?

(in reply to ebgreen)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Another function question Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts