Example
n1=Number(n1);
alert(“Byebye Covid-19”);
function test(message) {
alert(message); //→ This is a function body; message is an argument name.
}
"Defining a function does not execute it. Defining it names the function and specifies what to do when the function is called.
Calling the function actually performs the specified actions with the indicated parameters." by Mozilla
As a result, to execute the function above, we have to call it:test(“Byebye Covid-19”);
→ “Byebye Covid-19” is the argument inputted to the function.test(“Welcome to the 2.0 world”)
; → We can call the functions multiple times.
Example 1. a simple version
function add(n1,n2) {
alert(n1+n2);
}
add(1,100);
add(2,200);
=> result = 20402
Example 2. return function
function add(n1,n2) {
alert(n1+n2);
return n1+n2; //→ means it's finished
}
var result=add(1,100)*add(2,200);
alert(result);
=> result = 20402
function getSum(max){
sum=0
var n=1;
while(n<=max){
sum+=n;
n++;
}
alert(sum);
return sum;
}
var result=getSum(50)*getSum(100);
alert(result);
function getSum(max){
sum=0
for(var n=1;n<=max;n++){
sum+=n;
}
alert(sum);
return sum;
}
var result=getSum(50)*getSum(100);
alert(result);
=> result = 1275, 5050, 6438750
Feel free to comment and share your ideas below to learn together!
If you guys find this article helpful, please kindly do the writer a favor — LIKE this article.