0
Why is my function not Working??What am I doing wrong?
#include <iostream> using namespace std; int main() { SUM(); cout << a << endl; return 0; } int SUM() { int x=3; int y=4; int a=x+y; cout <<"The sum is:"<<a<<endl; } Can you guys tell me why this function dosent work?
10 ответов
+ 3
also the frist line of should be
int a = SUM();
not SUM();
+ 2
you need to add this code
return x+y;
in the function
and delete the last 2 lines of code
+ 2
since the function protype is int SUM();
that means the function will be returning a integer
+ 2
that is why you need the return statement
if you dont want to return nothing replace int with void then you dont need the return statement
+ 2
😏just take int a
because it is missing
other all right
+ 1
also add this line of code
int SUM();
under using namespce std;
+ 1
i never said you cant put cout in your function.
you could if you want .
0
Chris, can you explain me why it is necessary to put a return value and what does it do?Till now I dont understand what is value and what it does.An can you explain me why can I not put a cout instead of return?
0
Everything chris said
+
You need a prototype above main, because it does not know what SUM is.
Just put this above main:
int SUM();
alternatively you can put the function itself above main.
0
can u explain what is return on C++?And why cant I put cout instead of return?