Shoutbox

Call a Function within a Function - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Call a Function within a Function (/showthread.php?tid=64896)

Call a Function within a Function by Black_Ice on 08-14-2006 at 10:30 AM

I'd just like a little guidence with this one please. How (if you can) can I call a function in a function. The first function being outside the 2nd one. Example:

Function test1() {
...blah blah code.....
}

Function test2() {
call test1()
}

Thanks for any help. Sorry for the easy question.


RE: Call a Function within a Function by vikke on 08-14-2006 at 10:31 AM

code:
function test1() {
...blah blah code.....
}

function test2() {
test1();
}


RE: Call a Function within a Function by Intosia on 08-14-2006 at 10:52 AM

http://en.wikipedia.org/wiki/JavaScript


RE: Call a Function within a Function by Black_Ice on 08-14-2006 at 10:56 AM

Correct me if I'm wrong but isn't JavaScript different to the JScript we use in WLM P!


RE: RE: Call a Function within a Function by alexp2_ad on 08-14-2006 at 10:57 AM

quote:
Originally posted by Black_Ice
Correct me if I'm wrong but isn't JavaScript different to the JScript we use in WLM P!

Same kind of syntax, they are very, very close.
RE: RE: Call a Function within a Function by Intosia on 08-14-2006 at 10:58 AM

quote:
Originally posted by Black_Ice
Correct me if I'm wrong but isn't JavaScript different to the JScript we use in WLM P!

Well, its not -the same- but stuff like syntax and functions and stuff is the same, thats why i give you the link :)
RE: Call a Function within a Function by Black_Ice on 08-15-2006 at 08:25 AM

Ok thanks very much. For anyone seeking this answer. It's quite simple:

code:
function test1() {
blah..blah..code
};

function test2() {
test1();
};


RE: Call a Function within a Function by davidt on 08-15-2006 at 09:13 AM

Yes, functions are global and so can be called from anywhere. It's only the variables withinn functions that stay within the function.


RE: Call a Function within a Function by markee on 08-15-2006 at 09:16 AM

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.
Though you can carry it over to another function when you call upon that second function, you just have to put it between the brackets.
RE: Call a Function within a Function by davidt on 08-15-2006 at 09:35 AM

Yeah :) - I was talking about situations like

var test = 123;
function t() { Debug.Trace(test); }

Does anybody know if Jscript is pass-by-value of pass-by-reference? I think it is the latter.


RE: RE: Call a Function within a Function by vikke on 08-15-2006 at 09:42 AM

quote:
Originally posted by markee
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.
Though you can carry it over to another function when you call upon that second function, you just have to put it between the brackets.


When you're not talking about classes.
RE: Call a Function within a Function by Intosia on 08-15-2006 at 09:46 AM

Like this:

code:
function test1(value) {
    Debug.Trace(value)
};

function test2() {
   test1('hi');
};



I gave the wrong link, here you can find all stuff like that:
http://en.wikipedia.org/wiki/JavaScript_syntax

RE: Call a Function within a Function by vikke on 08-15-2006 at 09:56 AM

That's wrong?


RE: RE: Call a Function within a Function by CookieRevised on 08-15-2006 at 10:01 AM

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))