Why is my function not Working??What am I doing wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

21st Jul 2017, 9:44 PM
Kevin Paresh
10 Answers
+ 3
also the frist line of should be int a = SUM(); not SUM();
21st Jul 2017, 9:49 PM
chris
chris - avatar
+ 2
you need to add this code return x+y; in the function and delete the last 2 lines of code
21st Jul 2017, 9:46 PM
chris
chris - avatar
+ 2
since the function protype is int SUM(); that means the function will be returning a integer
21st Jul 2017, 9:56 PM
chris
chris - avatar
+ 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
21st Jul 2017, 9:58 PM
chris
chris - avatar
+ 2
😏just take int a because it is missing other all right
22nd Jul 2017, 12:34 AM
Irwin Lopez
Irwin Lopez - avatar
+ 1
also add this line of code int SUM(); under using namespce std;
21st Jul 2017, 9:54 PM
chris
chris - avatar
+ 1
i never said you cant put cout in your function. you could if you want .
21st Jul 2017, 10:02 PM
chris
chris - avatar
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?
21st Jul 2017, 9:51 PM
Kevin Paresh
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.
21st Jul 2017, 9:54 PM
Dennis
Dennis - avatar
0
can u explain what is return on C++?And why cant I put cout instead of return?
21st Jul 2017, 9:55 PM
Kevin Paresh