+ 1
Let me know #Q:1
What is the output of the following expression? Statement: function num(a,b) { var c = a*b;} num(2,6); Why answer is "Undefined"? In is tutorials sololearn told function have an optional "return" statement. if return is optional why answer is undefined. Reference is : https://www.sololearn.com/learn/JavaScript/1148/
3 Respostas
+ 3
you just forgot the return :
function num(a,b) {
return a*b;
}
num(2,6);
if you execute. function without something around, it will print undefined
+ 2
You see, the return statement is only optional if your function doesn't return anything. Then it is only to show that the function has ended.
But in your case, as it needs to return 'c', the return statement is absolutely necessary. Since you have not asked it to return anything, the output is "Undefined". Here's how you can fix your function:
function num(a,b) {
var c = a*b;
return c;
}
+ 1
A function can have an optional return statement. It is used to return a value from the function.
This statement is useful when making calculations that require a result.
As this code is making calculation, there is a need to use return statement.
https://www.sololearn.com/learn/JavaScript/1148/?ref=app