Basic Javascript tutorials-definitions and calls of functions-Basic javascript tutorials
A function is a statement that can run at any time. Simply put, a function is a set of statements that complete a function. It accepts 0 or multiple parameters.
Basic Function syntaxAs follows:
Copy codeThe Code is as follows:
Function functionName ([arg0, arg1,... argN]) {
Statement
[Return [expression]
}
Function is the keyword of the custom function, functionName is the function name, arg indicates the list of parameters passed to the function, and each parameter is separated by a comma. The parameter can be null.
Statement is a function of this province. It can be a variety of valid code blocks.
Reture expression is the return function value expression, which is also optional. A simple example is as follows.
Copy codeThe Code is as follows:
Function sayName (yname ){
Document. write ("hello" + yname)
}
SayName (1, 112 );
In addition, javascript will not be executed after rerurn is executed.
Copy codeThe Code is as follows:
<Div id = "xxx" style = "width: 200px; height: 100px; background-color: aquamarine"> </div>
<Script type = "text/javascript">
Function cNumber (inNmuber1, inNumber2 ){
Return inNmuber1 + inNumber2
}
Irese = cNumber (40, 20 );
Document. getElementById ("xxx"). innerHTML = irese;
</Script>
A function may have multiple return
Copy codeThe Code is as follows:
<Div id = "xxx" style = "width: 200px; height: 100px; background-color: aquamarine"> </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 the content of this article. Do you have a new understanding of defining and calling javascript? I hope this article will help you.