为什么我得到错误代码 ID 返回 1 退出状态我需要练习函数调用,但我很难调用 3 个函数。我需要了解如何在这个示例中正确调用函数。我需要声明一个主函数吗?根据练习,我认为我没有。
#include <iostream>
using namespace std;
/**
* Following function prints a message into the console
*/
void say_hello(){
cout << "Hello, World!" << endl;
}
/**
* The following function prints the sum of 2 integer numbers into the console, the numbers are
* passed as parameter
*/
void print_sum(int a, int b){
cout << "Sum is: " << a+b << endl;
}
/**
* The following function calculates the product of 2 integer parameter passed and returs the product to the caller.
*/
int getProduct(int a, int b){
int p = a * b;
return p;
}
void writeYourCode(int first, int second){
// Instruction 1: Please write a statement to call the function say_hello just after this line
say_hello();
// Instruction 2: Please write a statement to call the function print_sum and pass first and second as parameter
print_sum(first, second);
// Instruction 3: Please write a statement to call the function getProduct and pass first and second as parameter,
// catch the return value in an integer variable declared by you.
// Print that integer containing the result into console using cout, there must be a newline at the end.
// You need to print only the result and a new line
int total = getProduct(first,second);
cout << total << endl;
}
如之前的问题注释中所述,您需要实现一个main()
函数。简而言之,您的主要内容是程序的入口点。
因此,您已经正确实现了某些功能,但需要main()
才能使用它。它的结构如下:
int main() {
// you can call your functions from here!
}
关于main()
函数,有很多事情需要了解。您可以在此处找到所有详细信息。
你需要在所有函数定义之后编写一个main()函数。您可以使用 int main 或 void main。如果使用了 void main,则无需返回 0
int main(){
int first = 10;
int second = 3;
say_hello();
getProduct(5, 3);
print_sum(first, second);
return 0;
}