Function that returns a value
In the function in the previous section, the results are output through "document.write". What if you want to process the results of the function?
We only need to change the "document.write(sum)" line to the following code:
function add2(x,y)
{
sum = x + y;
return sum; //返回函数值,return后面的值叫做返回值。}You can also store the return value of the calling function through variables, the code is as follows:
result = add2(3,4);//语句执行后,result变量中的值为7。
Note: The parameters and return values in the function are not just numbers, but can also be other types such as strings
new file
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>返回值函数</title>
<script type="text/javascript">
function app2(x,y)
{ var sum,x,y;
sum = x * y;
return sum;
}
var req1 = app2(5,6);
var req2 = app2(2,3);
var sumq = req1 + req2;
document.write("req1的值:"+req1+"<br/>");
document.write("req2的值:"+req2+"<br/>");
document.write(req1+"与"+req2+"和:"+sumq);
</script>
</head>
<body>
</body>
</html>
Preview
Clear
- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
Let's briefly talk about starting a business in PHP
Quick introduction to web front-end development
Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
Login verification and classic message board
Computer network knowledge collection
Quick Start Node.JS Full Version
The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
















