Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Declaring functions without defining

    Just a quick question - can you declare functions without defining them, like in C++?

    eg:

    function foo(var firstarg, var secondarg);

    Then another function can call foo:

    foo(1,2);

    And foo is defined later down the page:

    function foo(var firstarg, var secondarg) { return firstarg+secondarg }
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, that would generate an error. Fortunately, unlike C/C++, there's no need to declare functions before they are called in the source with JavaScript. This is perfectly legal:

    foo(1,2);

    ...

    function foo(var firstarg, var secondarg) { return firstarg+secondarg }

  • #3
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sadly, that _seems_ to produce an error in IE. I will have another look at my code.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function foo(firstarg, secondarg) { return firstarg+secondarg; }
    Hope that helps!

    Happy coding!

  • #5
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,

    javascript may not have typing, but it does have arguments:

    x = foo(1,2);
    alert(x)
    x = foo(1,2,3)
    alert(x)



    function foo()
    {
    var retVal = 0;
    var argc = arguments.length;
    var argv = arguments;
    for (var i = 0; i < argc; i++)
    retVal += argv[i]
    return (retVal)
    }

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #6
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by nolachrymose
    Code:
    function foo(firstarg, secondarg) { return firstarg+secondarg; }
    Hope that helps!

    Happy coding!
    Sorry, was a typo in my reply... that's what I've got my functions like in my page.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!


  •  

    Posting Permissions

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