While you write your functions, somewhere you need to pass a few values to execute the function or in another case, your function body can execute on its own. And they are differ programmer to programmer though the logic behind the task is the same. Let's consider the above example, you have given,
what we need to achieve, a simple addition functionality.
block 1
var integer1 = 10;
var integer2 = 20;
function sum () {
sum = (integer1 + integer2);
return sum;
}
alert(sum());
to get the result you need to have integer1 & interger2 in body, as you already have. so your function's logic somewhere dependent on other elements. This is not a good practice as we build functions to handle a particular logic independent of the rest of the code.
So that within the entire execution process we can call that function and it always does the same kind of behavior.
block 2
var integer1 = 10;
var integer2 = 20;
function sum (integer1, integer2) {
sum = (integer1 + integer2);
return sum;
}
alert(sum(integer1, integer2));
Now, in this case, we are calling the same function but with parameters. In this scenario, an adder needs at least 2 values to add. So any point of time when we call this function gives us the result of the sum of the passing arguments.
So this function is not dependent on var integer1 & var integer2, if we pass some other variables to this function, we can get the same behavior.
Now we need to keep in mind when we call a function(as you do within "alert(sum());"), we need to check is that function requires any parameters, if so then we have to pass it as arguments, like,
// define our adding function
function sum (a, b) { // argument variables may have different variable names
sum = (a + b); // which only live within the function **scope**
return sum;
}
//calling sum
sum(integer1, integer2); // we already declared these two variables integer1 & integer2
// calling sum with direct values
sum(5, 5); // it returns 10
// now we have two new variables
var num1 = 50;
var num2 = 20;
sum(num1, num2); // as we expect it returns 70
why you got NaN
it is a language feature, as you using Javascript, any variable which is not defined holds a value undefined, you can say it is property of Javascript
NaN means not a number, when we execute addition operation the argument variables within the function expect themselves as number type variable, but, hence we didn't pass any parameters while calling the sum() function, integer1 & integer2 holds property of undefined, so you got NaN as a result.
as you can see, I pass two integer value 5, 5 to call sum, in another case num1, num2 integer type variables to call sum.
*If you take a close look on the last line, you see, you called alert(). It is a predefined function which we get out of the box of javascript programming language. But to do alert what it does, we need to pass a value, then only it can show the value in the alert box.
so while you call alert(sum(integer1, integer2)); (corrected code of yours)
it first executes sum and returns the value from it, then call alert using the return value and take it as an argument to call itself. After that, we get the alert box with the addition result.
Thanks, hope you'll get a bare minimum of clear idea about functions. It is an in-general concept, not just only for javascript.
sum()both are undefined, and the result is thereforeNaN.sum(10, 20).suminside the function as it will also overwrite the function of same namefunction sum(a, b) { return a + b; }The parameter names in the declaration are used to refer to the passed values; they shouldn't be the same as existing variables.