-2

To me, this code seems to have no errors, and it is correct in the way I learnt C++. What may be wrong?

This is my code:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
using namespace std;

double Calculation(long double x, long double y);
void Output(long double s, long double d, long double p, long double q);

void main(){

    long double a;
    long double b;
    long double sum;
    long double difference;
    long double product;
    long double quotient;
    cout << "Enter your first number." << endl;
    cin >> a;
    cout << "Enter your second number." << endl;
    cin >> b;

    Calculation(a, b);
    Output(sum, difference, product, quotient);


    system("pause");
}


double Calculation(long double x, long double y){
    long double sum;
    long double difference;
    long double product;
    long double quotient;
    sum = x + y;
    difference = x - y;
    product = x * y;
    quotient = x / y;
    return sum;
    return difference;
    return product;
    return quotient;
}

void Output(long double s, long double d, long double p, long double q){

    cout << "The sum of your numbers is " << s << "." << endl;
    cout << "The difference between your numbers is " << d << "." << endl;
    cout << "The product of your numbers is " << p << "." << endl;
    cout << "The quotient of your numbers is " << q << "." << endl;



}

Explanation: This is a calculator which works by the variables 'a' and 'b'. It calculates the sum, difference, product, and quotient of 'a' and 'b' by the function Calculate and outputs the answers with the function Output.

Error: uninitialized local variable 'quotient' used.
uninitialized local variable 'product' used.
uninitialized local variable 'difference' used.
uninitialized local variable 'sum' used.
ashatte
  • 5,442
  • 8
  • 39
  • 50
MerajA
  • 194
  • 5
  • 19

4 Answers4

5

Lots of things are wrong with your code, but there is a single root cause to this - a misunderstanding of how the return statement works.

You have a function with multiple return statements. It appears that you think that all of these statements would execute; that assumption is incorrect. Only the first return statement reached in a function is executed; the remaining ones are ignored.

Moreover, you appear to imply that return statement would influence variables in the caller automatically; it wouldn't. In order to modify a variable in the caller, the caller itself needs to assign the returned value.

If you need your function to return multiple values, you need to change the approach: it should take multiple arguments by reference, and modify them, like this:

void Calculation(long double x, long double y, long double &sum,
    long double &difference, long double &product, long double &quotient) {
    sum = x + y;
    difference = x - y;
    product = x * y;
    quotient = x / y;
}

You also need to change the prototype declaration of Calculation, like this:

void Calculation(long double x, long double y, long double &sum,
    long double &difference, long double &product, long double &quotient);

Call Calculation like this:

Calculation(a, b, sum, difference, product, quotient);

This will solve your compilation issue, and the code will run correctly.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The problem is that you declare the variables listed in the error message as local variables. That means that no other function will be able to use them. Declaring them again in another function declares new local variables.

In this case you might want to declare the variables as global variables. This is done by moving the definitions outside of any function, and only have that definition and not in a function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

In your main function you are not setting any values to those variables before passing them to the Output() function - therefore they are 'uninitialized'. Also, as mentioned in some comments, there are a number of other problems there too, here's a couple:

1) You can't do multiple returns in a function on the same logic path

2) You are not collecting the return of Calculation() anyway

I expect you can fix your issues by passing some of those variables by reference instead.

Alfie
  • 2,341
  • 2
  • 28
  • 45
1

As others said, the direct cause of your problem is misunderstanding on how the return and scoping works.

When dealing with C++ the compiler/linker warnings can be cryptic and/or confusing. In your example, the compiler should warn you about unreachable code after first return, however Visual Studio 2013 with default default does not do that.

You can make it do that by enabling all warnings, which is a good practice anyway. In project properties go to Configuration Properties -> C/C++ -> General -> Warning level and select EnableAllWarnings.

Final advice: if you are programming for fun or learning how to program, I'd advise you to start with C# or Java, which are easier and have better tool support.

ya23
  • 14,226
  • 9
  • 46
  • 43
  • Um, how is java easy... Everyone tells me it's terribly difficult. – MerajA Nov 26 '13 at 11:06
  • It's easier than C++, especially on a basic level. For starters, I'd definitely recommend C#. Combo of Visual Studio + Resharper + good book + StackOverflow makes learning it really easy compared to other languages. – ya23 Nov 26 '13 at 11:47
  • Um, I was learning a bit of C# from YouTube. I've noticed it's a bit similar to Visual Basic. I already know the basics of Visual Basic. Should I move on and improve my VB skills or should I stick to learning C#? – MerajA Nov 27 '13 at 12:38
  • I don't know VB myself, but it has a rather bad opinion. (For example see http://stackoverflow.com/questions/1653895/should-i-learn-vb-net-or-c ). – ya23 Nov 27 '13 at 13:43