简体   繁体   English

return语句显然不返回任何内容

[英]return statement apparently doesn't return anything

The return statement below doesn't output anything or errors, any reason why? 下面的return语句不输出任何内容或错误,为什么?

I tried Chrome and Firefox and repl.it. 我尝试了Chrome,Firefox和repl.it。

Here is the code I have: 这是我的代码:

//Create a function that uses a return statement
    function multiply(num1, num2){
     return num1 * num2;
    }
    //Call the function
    multiply(10,20 );

I don't get any output or errors. 我没有任何输出或错误。

if you return something, it means it returns something. 如果您退货,则表示它退货。 Right now, you only call multiply(), this function returns a value but you assign it to nothing. 现在,您仅调用multiple(),此函数将返回一个值,但您未将其分配为任何值。

If you use the following: 如果使用以下命令:

var returnValue = multiply(5, 10)

your returnValue variable contains the return value of the multiply function. 您的returnValue变量包含乘法函数的返回值。

You don't get any output because you haven't done anything to generate output. 您没有任何输出,因为您没有做任何事情来生成输出。 You aren't getting any errors because there are no errors in your code. 您不会遇到任何错误,因为您的代码中没有错误。 (It's okay to not use the return value of the function; in fact, it's quite common.) (不使用函数的返回值是可以的;实际上,这很常见。)

Just returning a value from a function doesn't output it anywhere, it just returns it from the function so you can use it in the code that called the function. 仅从函数返回值不会在任何地方输出它,而只是从函数返回它,因此您可以在调用该函数的代码中使用它。 One thing you might do with it is output it, but you also might just use it for something else, like including it in a calculation: 您可能会用它做的一件事就是输出它,但是您也可以将它用于其他用途,例如将其包括在计算中:

 function multiply(num1, num2){ return num1 * num2; } // Using the return value to output it: console.log(multiply(2, 3)); // Using it in another calcuation... const x = multiply(2, 3) + 4; // ...the result of which we might output if we like: console.log(x); 

But again, you might never output the result of a function. 但是同样,您可能永远不会输出函数的结果。 For instance, this code uses the result of the function document.getElementById to set up an event handler. 例如,此代码使用函数document.getElementById的结果来设置事件处理程序。 Doing that has no output at all: 这样做根本没有输出:

 let clickCounter = 0; document.getElementById("btn").addEventListener("click", function() { ++clickCounter; }); 
 <input type="button" value="Click Me" id="btn"> 

Unless you're not showing us all of your code, it's because you are not returning the value to anything. 除非你不向我们展示所有的代码,那是因为你没有返回值任何东西。 Functions that return a value need to have that value used somehow in order for it to be visible. 返回值的函数需要以某种方式使用该值才能使其可见。 Some examples might be: 一些示例可能是:

  1. Using it as a parameter in another method call (eg, logging it to the console): 在另一个方法调用中将其用作参数(例如,将其记录到控制台):
console.log(multiply(10,20));
  1. Setting it to a variable: 将其设置为变量:
var result = multiply(10,20);
  1. Using it as a value in another piece of logic: 在另一个逻辑中将其用作值:
document.getElementById("equation").textContent = "10 x 20 = " + multiply(10,20);

Without actively capturing and/or using the return value of a method, it will not be displayed anywhere. 如果没有主动捕获和/或使用方法的返回值,它将不会在任何地方显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM