Не удается вернуть простую функцию в C ++ - проблема с синтаксисом - PullRequest
0 голосов
/ 04 января 2019

Почему я получаю код ошибки id return 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;



    }

Ответы [ 2 ]

0 голосов
/ 04 января 2019

Вам нужно написать функцию 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;
}
0 голосов
/ 04 января 2019

Как уже отмечалось в ваших комментариях к вопросу, вам необходимо реализовать функцию main().Проще говоря, ваша основная точка доступа - это точка входа для вашей программы.

Таким образом, вы правильно реализовали некоторые функции, но для их использования требуется main().Он имеет следующую структуру:

int main() {
    // you can call your functions from here!
}

Есть много вещей, которые нужно знать о функции main().Вы можете найти все детали здесь .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...