A function is a statement that can be run at any time. Simply put, a function is a group of statements that complete a certain function, which accepts 0 or more parameters.
The basic syntax of the function is as follows
The code copy is as follows:
function functionName([arg0,arg1,......argN]){
statement
[return[expression]]
}
Where, function is the keyword of a custom function, functionName is the function name, arg represents the list of various parameters passed to the function, and each parameter is separated by commas. The parameter can be empty.
The statement is the function province and can be various legal code blocks.
The return function is the value of the expression, which is also optional. A simple example is as follows.
The code copy is as follows:
function saysName(yname){
document.write("hello"+yname)
}
sayName(112);
In addition, JavaScript will no longer be executed after rerurn.
The code copy is as follows:
<div id="xxx"></div>
<script type="text/javascript">
function cNumber (inNmuber1 ,inNumber2) {
return inNmuber1 + inNumber2
}
irese = cNumber(40,20);
document.getElementById("xxx").innerHTML = irese;
</script>
There may be multiple returns in a function
The code copy is as follows:
<div id="xxx"></div>
<script type="text/javascript">
function cNumber (inNmuber1 ,inNumber2) {
if (inNmuber1 >= inNumber2)
return inNmuber1 - inNumber2
else
return inNumber2 - inNmuber1
}
irese = cNumber(10,20);
document.getElementById("xxx").innerHTML = irese;
</script>
The above is all about this article. Have you gained a new understanding of defining and calling JavaScript? I hope this article can be helpful to everyone.