1

I'm pretty new coding C++, but still practicing it.

I have the following code of main.cpp file:

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

int getValueFromUser() {
    std::cout << "Enter an integer: ";
    int input{};
    std::cin >> input;

    return input;
}

void printDouble(int value) {
    std::cout << value << " doubled is: " << value * 2 << "\n";
}

int main() {
    printDouble(getValueFromUser());
    addNumber(inputFirstNumber(), inputSecondNumber());

    return 0;
}

The following code of additionalFile.cpp file:

#include <iostream>

int inputFirstNumber() {
    std::cout << "Enter first integer: \n";
    int num;
    std::cin >> num;

    return num;
}

int inputSecondNumber() {
    std::cout << "Enter second integer: \n";
    int num;
    std::cin >> num;

    return num;
}

void addNumber(int x, int y) {
    std::cout << "Your result is " << x + y << "\n";
}

And code for additionalFile.h file:

#pragma once
int inputFirstNumber(); int inputSecondNumber(); void addNumber(int x, int y);

The question is: Why, when i run the program and it runs addNumber() function, does it ask me for the second digit to input, and then for the first one? The order looks misordered.

Thank you in advance!

addNumber(inputFirstNumber(), inputSecondNumber());

In this line i've exchanged parameters vice versa, and it asked me to input digits in proper order. But i want to get the basic clue of the issue.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • It is simply unspecified, the compiler can evaluate them as it sees fits. The only guarantee you get the calls are not interleaved and are evaluated before the call itself. – Quimby Jan 04 '23 at 14:03
  • 2
    The order of parameter evaluation is arbitrary. https://en.cppreference.com/w/cpp/language/operator_other – Richard Critten Jan 04 '23 at 14:04
  • if you want the order to be defined, use something like: int a = f1(); int b = f2(); int c = calc( a, b ); – Neil Butterworth Jan 04 '23 at 14:15

0 Answers0