www.webdeveloper.com
Results 1 to 3 of 3

Thread: passing variables between functions

  1. #1
    Join Date
    Nov 2002
    Posts
    2

    passing variables between functions

    i know this question is relatively easy, but please do help me

    i need to pass a variable from two functions, i.e.

    function smile(big, happy){
    me=happy;
    .
    .
    .}
    function sad(){
    alert(me);
    }

  2. #2
    Join Date
    Nov 2002
    Posts
    7
    webmark,

    to pass a value from one function to another,
    pass the value as a parameter of the function call.

    as follows:
    ---------------------------------------------------
    function smile () // function header
    {
    var me; // variable declaration

    sad (me); // function call ( with parameter )

    return; // return program control to calling function
    }

    function sad (me) // function header
    {
    alert (me); // output value to the terminal

    return; // return program control to calling function
    }
    ----------------------------------------------------

    if you simply need to access a variable from inside of multiple functions,
    declare the variable outside of all functions, thus making it a global variable.
    then you will be able to access it from with any subsequent funcitons

    as follows:
    -----------------------------------------------------
    var me;

    function smile ( )
    {
    me = 'big';

    alert (me);

    return;
    }

    function sad ()
    {
    me = 'happy';

    alert (me);

    return;
    }
    ------------------------------------------------------

    hope this helps

    ritz

  3. #3
    Join Date
    Nov 2002
    Posts
    421
    I think there is a simple answer to your question.

    If you have defined a variable, you can use it in as many functions as you want. Its value stays at what ever it was last. You will note that you generally define a variable outside of the function in the first place, then use it in a function. There is no association between a function and a variable per se. So feel free to use a variable in more than one function.

    <script . . . .
    var whatever
    function number1()
    ...
    function number2()
    ...
    </script>

    both functions can use variable whatever

    Is that what you are asking?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles