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
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?
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:
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
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.
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.
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 >
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
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.