In C++, why does declaring a different return type for function definition and declaration change the output of the function?

Viewed 74

I have three files as follows:

main.cpp

#include <iostream>
#include "add.h"

using namespace std;

int main()
{
    cout << add(3, 4) << "\n" << add(3,4) << endl;
    return 0;
}

add.h

double add(int a, int b);

add.cpp

int add(int a, int b)
{
    return a + b;
}

This compiles and the output seems very weird to me:

4.6836e-310
0

I would never do this in practice but am trying to learn how the linkage editor and compiler work. Firstly, why is the output for the first not 7? Secondly, why does the same function return 0 when called again?

0 Answers
Related