<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>函数</title>
<script type="text/javascript">
// console.log("数据校验第一步");
// console.log("数据校验第二步");
// console.log("数据校验第三步改变");
// console.log("数据校验第四步");
// console.log("数据校验第一步");
// console.log("数据校验第二步");
// console.log("数据校验第三步改变");
// console.log("数据校验第四步");
// console.log("数据校验第一步");
// console.log("数据校验第二步");
// console.log("数据校验第三步改变");
// console.log("数据校验第四步");
// function myname(){
// console.log("我是函数体");
// }
// myname();
// datacheck();
// datacheck();
// datacheck();
//创建一个新的函数(声明函数或者定义函数)
// function datacheck(){
// console.log("数据校验第一步");
// console.log("数据校验第二步改变");
// console.log("数据校验第三步改变");
// console.log("数据校验第四步");
// }
// function add(){
// var a = 100;
// var b = 200;
// var res = a + b;
// }
//名字 参数 返回值
function add(arg1,arg2){
var res = arg1 + arg2;
// console.log(res);
return res;
}
var res1 = add(100,200);
var res2 = add(80,90);
console.log(res1);
console.log(res2);
</script>
</head>
<body>
</body>
</html>
什么是函数?
一个函数有自己一个固定的功能,调用函数相当于调用这个功能。
函数有系统内置的函数,我们也可以定义自己的函数,使用这个函数实现我们想要的功能。
在定义函数的时候,我们需要写很多行代码来实现我们想要的功能。
函数可以被多次调用,我们只需要通过函数名调用即可调用响应的功能,这样就避免了每次想要调用某个功能的时候,就去书写重复的代码。
函数的作用:
1,一个函数实现一个固定的功能
2,避免重复写代码
函数怎么调用?
xxx1(arg1,arg2,arg3);
函数名是定义的时候决定的,参数也是定义的时候决定的。我们想要什么功能就必须调用对应名字的函数,并且提供对应的参数。(调用别人的函数的时候,不能自己随便写函数名和参数)