1

I have been learning C++ using VS Code as the IDE and MingW as the compiler. While writing a code I encountered a problem where the output I got didn't matched with what I expected it to be. Here's the code I wrote :

#include<iostream>
using namespace std;

int sum(int a, int b){
    cout<<"Using function with 2 arguments"<<endl;
    return a+b;
}

int sum(int a, int b, int c){
    cout<<"Using function with 3 arguments"<<endl;
    return a+b+c;
}

int main() {
    cout<<"The sum of 3 and 6 is "<<sum(3,6)<<endl;
    cout<<"The sum of 3, 7 and 6 is "<<sum(3, 7, 6)<<endl;

    return 0;
}

The output I got was this :

using function with 2 arguments 
The sum of 3 and 6 is 9
using function with 3 arguments
The sum of 3, 7 and 6 is 16

But I believe the output should be :

The sum of 3 and 6 is Using function with 2 arguments   
9
The sum of 3, 7 and 6 is Using function with 3 arguments
16

I tried copy pasting it on a online editor and it did worked there correctly. Is there some issue with my compiler ? If yes, then how can I fix it ?

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 2
    Which version of C++ are you using? – chris Nov 13 '21 at 14:31
  • @chris -- unfortunately, we can't downvote comments. The version makes absolutely no difference here. Any answer will address the differences in different versions, regardless of which version is being used for the code in the question. That's the kind of unhelpful comment that makes newcomers reluctant to post. – Pete Becker Nov 13 '21 at 14:55
  • The compiler is calling `sum(3, 6)` before calling the stream inserter that writes `"The sum of 3 and 6 is "`. Back in the olden days this was allowed; there have been some recent changes that might have affected this (I haven't been paying attention lately), but, in general, when an expression involves multiple function calls (`<<` and `sum(3,6)`, don't count on them being called in any particular order. More specifically, writing output from a function that's called in the middle of writing some other output might, as you've seen, give surprising results. – Pete Becker Nov 13 '21 at 15:00
  • @PeteBecker, It's information that should be present in the question, especially when discovering a difference between two setups. An answer can mention the change, but can focus on the relevant version, which is the one the OP cares about. At the end of the day, it's simply a question asking for some relevant information, which is the first thing listed in the comment placeholder text. – chris Nov 13 '21 at 15:05
  • Very unlikely that a compiler creates the wrong executable from a C/C++ source file written by a novice programmer. It always is a coding error by the novice programmer. – rioV8 Nov 13 '21 at 15:08
  • Don't do side effects in functions (that calculate stuff). – rioV8 Nov 13 '21 at 15:11
  • @chris -- if someone wrote an answer that **only** said "yes, that's okay, because the order is unspecified", it would get downvoted because it didn't mention that this may have changed. Besides, it's **obvious** that this was compiled with an earlier version that didn't have the new requirements. Confirming that just doesn't matter.. – Pete Becker Nov 13 '21 at 16:18
  • @rioV8 what should be the correct code ? can you show me .? ............. – Shubham Prakash Nov 14 '21 at 08:49
  • @PeteBecker so what is going wrong here ? – Shubham Prakash Nov 14 '21 at 08:50
  • @chris it's just been a week or so since I downloaded vsc and mingw and learning so I don't know much about how the version makes differences.......... – Shubham Prakash Nov 14 '21 at 08:52
  • @ShubhamPrakash, I don't expect you to know the differences between versions unless you've found some as part of researching a problem. What I've been saying here is that which version you're using is often useful information when there's a problem. – chris Nov 14 '21 at 09:22
  • Anyway, what those duplicate links are saying is that you're never guaranteed to get the output you expected when you have output nestled within your larger output statement. That also applies to other side effects like changing a variable. The way around that is to separate out the output you want to have first and put it before the other one. In this case, move the output from the functions to `main` and put them before the output statements that call the functions. That way there's no room for doubt on what will be output when reading the code. – chris Nov 14 '21 at 09:39
  • As a bonus, once the `sum` functions don't have output in them, they do what their name suggests to anyone reading it: sum the arguments. There are no longer any surprises to be had when a function goes to sum some numbers and ends up with output that it didn't want. Having functions do only their main task and avoid doing extra stuff is a good habit to get into because it makes the function more reusable and less surprising. – chris Nov 14 '21 at 09:43

0 Answers0