My Code doesnt work when I use cout inside a function but if I return the value and stream inside main function it works. My code which does not work is
#include <iostream>
#include <cstring>
using namespace std;
int add(int a,int b){
int c = a+b;
cout<<c<<endl;
}
string add(string m,string n){
string c = m+" "+n;
cout<<c<<endl;
}
int main(){
string x ="amit";
string y ="kumar";
add(x,y);//adding string
add(5,58);//adding numbers
}
But when I return the value it works fine
#include <iostream>
#include <cstring>
using namespace std;
int add(int a,int b){
int c = a+b;
cout<<c<<endl;
}
string add(string m,string n){
string c = m+" "+n;
return c;
}
int main(){
string x ="amit";
string y ="kumar";
string z = add(x,y);
cout<<z<<endl;
add(5,58);//adding numbers
}
I am using Codeblocks for my programming. Why is this abrupt behaviour. What am I doing wrong here.