Return cannot have a parameter in function returning void

The error “return cannot have a parameter in function returning void” occurs when a function is defined with a return type of void (meaning it does not return any value) and the programmer tries to include a parameter in the return statement.

In most programming languages, void is used as a return type for functions that do not return a value. These functions are typically used for performing actions or operations without the need to retrieve a result. Since they don’t have a return value, it is not valid to include a parameter in the return statement.

Here’s an example that demonstrates this error in JavaScript:


    function exampleFunction() {
      console.log("Hello, World!");
      return "example"; // Error: cannot have a parameter in function returning void
    }
  

In this example, the function exampleFunction is defined with a return type of void (since there is no explicit return type specified in JavaScript functions). However, the programmer mistakenly included a parameter (“example”) in the return statement. This results in an error because the function is not expected to return any value.

To resolve this error, you should remove the parameter from the return statement or change the return type of the function to an appropriate type that matches the value being returned.

Read more

Leave a comment