Den
Den

Reputation: 75

How to call a number from another function into a function?

How can I call the "x" from function numOne into function numTwo? Is there any simpler way to do this? Or how can I call the result from numOne into numTwo?

<!doctype HTML>
<html> 
    <head> 
        <title>Function Testing</title>
    </head> 
    <body>
        <script type="text/javascript">
            function numOne(x, y){
                var x = 3;
                var y = 4;
                var result = x+y;
                numTwo();
            }
            function numTwo(){
                alert(x);
            }
        </script>
    </body> 
</html>

Upvotes: 0

Views: 76

Answers (2)

Enver Evci
Enver Evci

Reputation: 184

You can do with global value

var x;
function numOne(x, y){
    x=3;
    var y = 4;
    var result = x+y;
    numTwo();
}
function numTwo(){
    alert(x);
}

or with adding a parameter to numTwo function

function numOne(x, y){
    var x = 3;
    var y = 4;
    var result = x+y;
    numTwo(x);
}
function numTwo(t){
    alert(t);
}

Upvotes: 3

dashtinejad
dashtinejad

Reputation: 6253

Pass the result to numTwo:

function numOne(x, y){
    var result = x+y;
    numTwo(result);
}

function numTwo(r){
    alert(r);
}

numOne(2, 3);

Upvotes: 3

Related Questions