Home > Backend Development > C++ > body text

C++ syntax error: function has no return value, how should I modify it?

WBOY
Release: 2023-08-22 16:23:02
Original
2021 people have browsed it

C++ syntax error: function has no return value, how should I modify it?

Today, we will take a look at a common problem in C programming-the syntax error caused by a function not returning a value, and how to modify it.

In C programming, we often need to define functions and call them at different locations in the program. At this time, we need to pay attention to the return value of the function. If a function is defined to have a return value, then a corresponding value must be returned after the function is executed. Otherwise, the compiler will issue an error saying "Function has no return value".

Now, let’s look at a simple example:

#include <iostream>
using namespace std;

int sum(int a, int b) {
    int c = a + b;
}

int main() {
    int a = 1, b = 2;
    int result = sum(a, b);
    cout << result << endl;
    return 0;
}
Copy after login

In the above code, we define a function sum, which is used to calculate the sum of two integers. However, in the implementation of the function, we only calculated the sum of two numbers but did not return it. Therefore, when we compile, the compiler will prompt an error:

错误 C4716: 'sum': 必须返回值
Copy after login

To fix this error, we can add a return value to the function sum. In this example, we need to return the sum of two numbers, so we can change the function declaration to the following form:

int sum(int a, int b) {
    int c = a + b;
    return c;
}
Copy after login

At this time, when we compile the code again, we will avoid the "function has no return value" syntax mistake.

In addition to adding return values ​​to functions, we can also use the void keyword to define functions without return values. The void keyword is required in both function declaration and function definition. The modified code is as follows:

#include <iostream>
using namespace std;

void printMessage() {
    cout << "Hello World!" << endl;
}

int main() {
    printMessage();
    return 0;
}
Copy after login

In this example, we define a function printMessage with no return value to output a message. It should be noted that there is no need to add a return statement in the function body because the function itself has no return value.

In short, whether you define a function with a return value or a function without a return value, you need to pay attention to its syntax structure when writing code to avoid the error "function has no return value".

The above is the detailed content of C++ syntax error: function has no return value, how should I modify it?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template