SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling a Function

    I would like to call a function within a function. Is this even possible??
    When I do this I get "undefined"

    Here's my code:
    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    function product(a,b){
       total = a*b;
       display(total);
    }
    
    function display(num){
       return num;
    }
    </script>
    </head>
    
    <body>
    <h1>This text I do NOT want to be erased.</h1>
    
    <a href="http://google.com" target="_blank">Total: </a>
    <script type="text/javascript">
    product(3,4);
    document.write(display());
    </script>
    
    </body>
    </html>
    I know you can just return in the first function, "product()" but I would like to do something else in my second function "display()" then return a value.

    thanks

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Multi-posted. People don't appreciate cross posting of questions in multiple forums, particularly Mickey Mouse queries like this one.
    Tab-indentation is a crime against humanity.

  3. #3
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    document.write(display());
    you are calling display() without passing a parameter to it as expected by your function declaration

    Code:
    function display(num) {
    The fix should be obvious


Bookmarks

Posting Permissions

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