jeudi 2 juillet 2015

C++ function about usage of return

Hello i am totally new to c++ programming, I had a question when we use a int function why do we have to use return command like we can use cout << sum << endl; and call the function in main. but for return we have to do like cout << printSum();

Case 1:

#include <iostream>
using namespace std;

int addNumbers(int x, int y){
    int sum = x + y;
    cout << sum << endl;
}

int main() {
    addNumbers(4, 5);
    return 0;
}// without using  return

Case 2:

#include <iostream>
using namespace std;

int addNumbers(int x, int y){
    int sum = x + y;
    return sum;
}

int main() {
    cout << addNumbers(4, 5) << endl;
    return 0;
}// return method

Aucun commentaire:

Enregistrer un commentaire