What is the point in returning my variable?

So this is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream>

using namespace std;

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

int main()
{
    cout<<numbers(1,2)<<endl;
return 0;
}


in the numbers function. what is the point in returning "sum." I mean the program still works if you don't return sum.
It will return an integer and it may not the one you want in another case or with future compilers. Your compiler should have given a warning if there was no return statement.
A simpler way,
1
2
3
4
int numbers(int a, int b)
{
    return a+b;
}

Last edited on
what is the point in returning "sum."

The point is to return the result to the caller.

the program still works

The program will run, but it won't give the correct answer.
If you omit line 7, the program will compile with a warning, but the cout won't be correct.
The function will return 0, which of course, is not the result of adding 1+2.
Last edited on
Topic archived. No new replies allowed.