What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Call a Function within a Function

Call a Function within a Function
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: RE: Call a Function within a Function
quote:
Originally posted by davidt
Yes, functions are global and so can be called from anywhere. It's only the variables withinn functions that stay within the function.
functions can be perfectly local to another function too.

Just as variables can be global to all functions too. ;)

code:
var Globe1 = "Hello World"

function DoTheBanana() {
        var Localfruit = "Hello bananatown";
        Debug.Trace('Localfruit: ' + Localfruit);
}

function DoTheConga() {
        // Localfruit isn't valid in this function since it is declared locally inside DoTheBanana()
        // It's scope begins and ends in the function DoTheBanana()
        // Globe1 is valid in this function since it is declared globally.
        Debug.Trace('Globe1: ' + Globe1)
        var Localvar = "Hello bananatown";

        // Notice that we can declare a local variable to this function and that it will keep its value, even if have another local variable with the same name inside the DoTheBanana() function.
        var Localfruit = "I hate bananas";
        DoTheBanana();
        Debug.Trace('Localfruit: ' + Localfruit);
}

function NowForSomethingDifferent() {
        // Let's create a local function which is only valid inside NowForSomethingDifferent()
        function WoowLocalFunction() {
                Debug.Trace('This is a local function')
        }
        // And call it
        WoowLocalFunction();
}
Tip: try to not use global variables, unless it is absolutely nessecairly (goes for all languages you program in).

-------------------------------------------------------------------------

quote:
Originally posted by davidt
Does anybody know if Jscript is pass-by-value of pass-by-reference? I think it is the latter.
Numbers and Boolean values (true and false) are copied, passed, and compared by value. When you copy or pass by value, you allocate a space in computer memory and copy the value of the original into it. If you then change the original, the copy is not affected (and vice versa), because the two are separate entities.

Objects, arrays, and functions are copied, passed, and compared by reference.** When you copy or pass by reference, you essentially create a pointer to the original item, and use the pointer as if it were a copy. If you then change the original, you change both the original and the copy (and vice versa). There is really only one entity; the "copy" is not actually a copy, it's just another reference to the data.

When comparing by reference, the two variables must refer to exactly the same entity for the comparison to succeed. For example, two distinct Array objects will always compare as unequal, even if they contain the same elements. One of the variables must be a reference to the other one for the comparison to succeed. To check if two Arrays hold the same elements, compare the results of the toString() method.

strings are copied and passed by reference, but are compared by value. Note that if you have two String objects (created with new String("something")), they are compared by reference, but if one or both of the values is a string value, they are compared by value.


Note: When you pass a parameter to a function by value, you are making a separate copy of that parameter, a copy that exists only inside the function. Even though objects and arrays are passed by reference, if you directly overwrite them with a new value in the function, the new value is not reflected outside the function! Only changes to properties of objects, or elements of arrays, are visible outside the function.


**(To be very stricly correct that isn't entirly true though (JScript internally still makes a copy; but it also internally copies back the changed copy; aka: it isn't 'by reference' as in other languages <= hard to explain, though you shouldn't be bothered with it either, it's just per-info))

This post was edited on 08-15-2006 at 10:17 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
08-15-2006 10:01 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Call a Function within a Function - by Black_Ice on 08-14-2006 at 10:30 AM
RE: Call a Function within a Function - by vikke on 08-14-2006 at 10:31 AM
RE: Call a Function within a Function - by Intosia on 08-14-2006 at 10:52 AM
RE: Call a Function within a Function - by Black_Ice on 08-14-2006 at 10:56 AM
RE: RE: Call a Function within a Function - by alexp2_ad on 08-14-2006 at 10:57 AM
RE: RE: Call a Function within a Function - by Intosia on 08-14-2006 at 10:58 AM
RE: Call a Function within a Function - by Black_Ice on 08-15-2006 at 08:25 AM
RE: Call a Function within a Function - by davidt on 08-15-2006 at 09:13 AM
RE: RE: Call a Function within a Function - by CookieRevised on 08-15-2006 at 10:01 AM
RE: Call a Function within a Function - by markee on 08-15-2006 at 09:16 AM
RE: RE: Call a Function within a Function - by vikke on 08-15-2006 at 09:42 AM
RE: Call a Function within a Function - by davidt on 08-15-2006 at 09:35 AM
RE: Call a Function within a Function - by Intosia on 08-15-2006 at 09:46 AM
RE: Call a Function within a Function - by vikke on 08-15-2006 at 09:56 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On