Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Exercise

Name: Anonymous 2008-02-13 11:24

Hi.

I'm attending a C++ programming course. The teacher gave us some exercises, in which we must find what's wrong with the code.
The problem is I'm completly stuck in one of them. I tried to create and compile the following code:
#include<iostream>

using namespace std;

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

void main()
{
    cout << "2 + 3 = " << my_sum(2,3) << endl;
}


The problem is the compiler gives no errors and the program runs without any errors either.
Could someone PLEASE help me?
Thx in advance.

Name: Anonymous 2008-02-13 12:00

I made some changes to my code, so that it looks like this (still no return).

#include<iostream>

using namespace std;

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


int main(){
    cout << "1 + 3 = " << my_sum(1,3) << endl;
    cout << "2 + 3 = " << my_sum(2,3) << endl;
    cout << "3 + 3 = " << my_sum(3,3) << endl;
    cout << "4 + 3 = " << my_sum(4,3) << endl;   
   
    return 0;
}


and this is what happened:
$ g++ teste.cpp
$ ./a.out
1 + 3 = 4
2 + 3 = 5
3 + 3 = 6
4 + 3 = 7


So, does this proves that no return is required?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List