function with p...LOGIN

function with parameters

The add2() function in the previous section cannot add any two specified numbers. In fact, the function definition can also be in the following format:

function 函数名(参数1,参数2)
{
     函数代码
}

Note: There can be multiple parameters, increase or decrease the number of parameters as needed. Parameters are separated by (comma,).

According to this format, the function to implement the sum of any two numbers should be written as:

function add2(x,y)
{
   sum = x + y;
   document.write(sum);
}

x and y are the two parameters of the function. When calling the function, we can pass these two The parameters pass the two actual addends to the function.

For example, add2(3,4) will find the sum of 3+4, and add2(60,20) will find the sum of 60 and 20.

Next Section
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>函数传参</title> <script type="text/JavaScript"> function add3(x,y,z) { sum = x + y +z; document.write(x+"、"+y+"、"+z+"和:"+sum+"<br/>"); } add3(5,8,3) ; add3(7,1,4); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware