-1

Why do i keep getting uninitialized local vairables in the second function even though i already did initialized them?

Error C4700 uninitialized local variable 'x1'
Error C4700 uninitialized local variable 'x2'

#include <iostream>
using namespace std;
bool is_prime(int n, bool is_prime = 0)
{
    for (int i = 2; i < n; i++)
    {
        bool is_prime = true;
        if (n % i == 0)
            is_prime = false;
    }
    return is_prime;
}

int sumPrime(int , int )
{
    int x1, x2; // keeps saying its unitialized

    int sum = 0;
    for ( x1; x1 < x2; x1++)
    {
        if (is_prime(x1))
        {
            sum += x1;
        }
    }
    return sum;
}

int main()
{
    int n1, n2;
    cout << " enter two ns" << endl;
    cin >> n1 >> n2;
    cout << sumPrime(n1, n2) << endl;
    return 0;
}
mch
  • 9,424
  • 2
  • 28
  • 42
Sarah_Xx
  • 123
  • 2
  • 10
  • 4
    How are `int x1, x2; ` initialised? –  Nov 08 '18 at 16:39
  • as integers? i dont get the question sorry! – Sarah_Xx Nov 08 '18 at 16:40
  • `bool is_prime = true;` is a whole new `is_prime` and hides the argument. – François Andrieux Nov 08 '18 at 16:42
  • 7
    @Sarah_Xx looks like you might benefit from the good C++ book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?s=1|994.2496 – SergeyA Nov 08 '18 at 16:42
  • 3
    It looks like you might be thinking that `int x1, x2;` are initialized to the arguments of `sumPrime` but in fact `sumPrime` has two unnamed and unused parameters which are set to the arguments' values, and `x1` and `x2` are left uninitialized. – François Andrieux Nov 08 '18 at 16:43
  • `int sumPrime(int , int )` on a function definition means roughly "you need to pass me two `int`s but I dont care what their values are (and i have no way to access them)" – 463035818_is_not_an_ai Nov 08 '18 at 16:44
  • Your second function has unnamed arguments - did you mean to accept x1 and x2 in the argument list (and therefore initialize them)? – dmcgrandle Nov 08 '18 at 16:44
  • I highly recommend using different names between variables and functions. In your `is_prime()` function, you declare a `is_prime` variable in the `for` loop. – Thomas Matthews Nov 08 '18 at 16:52
  • BTW, you should move your declaration of `bool is_prime = true` before the `for` loop in the `is_prime()` function. Otherwise, the `is_prime` variable is local to inside the loop and will be destroyed after each iteration in the loop. – Thomas Matthews Nov 08 '18 at 16:53

2 Answers2

4

Your curious parameter syntax

int sumPrime(int , int )
{
    int x1, x2;

dates from pre-standardised C (although I don’t know of an old compiler that would have compiled this particular variant) and it never made it into C++.

You need

int sumPrime(int x1, int x2)
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    Nice, I would not have thought of that. But, was it not `int sumPrime(int,int) int x1, x2 { ... }` or something like that? – YSC Nov 08 '18 at 16:48
  • Even in good ole c it would be incorrect, naming parameters would have to be done before opening brackets, like `int baz (x, y) int x, y {...}` – SergeyA Nov 08 '18 at 16:52
  • This is it. It rings bells now. There were usually a semi-colon before the opening bracket. – YSC Nov 08 '18 at 16:56
  • The old syntax was `int baz() int x, y { ... }` – Barmar Nov 08 '18 at 17:01
  • 1
    Since it wasn't standardized, I guess each compiler family had its own syntax. But the K&R book was an informal spec at the time, right? So clearly, [`baz(x,y) int x, int y; { /* ... */ }`](https://archive.org/details/TheCProgrammingLanguageFirstEdition/page/n29) – YSC Nov 08 '18 at 17:17
  • The one I remember had the declarations before the opening brace - so the way the OP has it is a variant. But to me it looks like they are attempting to follow the old way, albeit in error: perhaps in an attempt to get something to compile on a C++ compiler. – Bathsheba Nov 08 '18 at 17:22
3

There are various problems inside your code:

The is_prime() function should be like:

bool is_prime(int n) // <-- No use of 'is_prime' parameter here...
{
    bool is_prime = true; // should be outside the loop...
    for (int i = 2; i < n; i++)
    {
        if (n % i == 0)
            is_prime = false;
    }
    return is_prime;
}

and the sumPrime() function:

int sumPrime(int x1, int x2)
{
    /*int x1, y1; Just forget these variables, they are not even initialized*/
    int sum = 0;
    for ( /*x1 <- It is not syntatically correct, it is not an expression, so just leave it blank*/; x1 < x2; x1++)
    {
        if (is_prime(x1))
        {
            sum += x1;
        }
    }
    return sum;
}

Explanation:

The is_prime() function..., what you have done here is that you have declared the variable is_prime (Not the function, look closely) both in the parameters and also inside the loop...

This, in fact, will not cause a problem, but will shadow your previous declaration...

Also, there is no need for is_prime to be present in the parameters because it is mostly useless (Maybe because I don't know what you are trying to achieve)... But you have to choose one, so there is something like this you can do:

bool is_prime(int n, bool& is_prime) // Make is_prime a reference and assign to it...

Also, change this line:

bool is_prime = true;

to:

is_prime = true; // Remove bool specifier, don't declare 'is_prime' again!

As for your other function, it is, in fact, not even an old syntax, and don't even ask about C++, the only way you can declare functions in C++ is:

<return_type> <function-name>(<parameters>) { <body> }

Note that this is pseudo-syntax of a function declaration and is followed by most languages nowadays...

So your function should also look like:

bool is_prime(int x1, int x2) { /* <body> */ }

And also remove the declarations of x1 and x2 inside the function to prevent variable shadowing (Just like the above example)...


Edit: Also, Looking at these small mistakes, anyone will tell you to look at a good C++ book...

Ruks
  • 3,886
  • 1
  • 10
  • 22
  • i am confused? isnt initialized meaning declaring them as a variable? so if i wrote int x1, x2; i would then initialized them then right? – Sarah_Xx Nov 08 '18 at 17:38
  • 1
    No, declaring them doesn't mean they are initialized, when you declare them, they are not assigned any values, and since these variables are not **classes**, they are not having any default values either and your program runs on [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior), see [what happens when you don't initialize a variable](https://stackoverflow.com/questions/30172416/uninitialized-variable-behaviour-in-c)... – Ruks Nov 08 '18 at 17:41
  • yeah but wont they be assigned to the value once i call the function? thats why i didnt declare it only intiaizlied it – Sarah_Xx Nov 08 '18 at 17:44
  • I don't see any attempts of an assignment in your code and I don't fully get what you mean either... – Ruks Nov 08 '18 at 17:46
  • i mean i didnt give x in sumprime any value because in main i will give it a value – Sarah_Xx Nov 08 '18 at 17:50
  • 1
    No no, see, you have declared those variables **inside the `sumPrime()` function** and hence it is a *local variable* and *local variables* **can't** be accessed using any other function outside the *function where they are defined*... – Ruks Nov 08 '18 at 17:52
  • what about bool_prime? i mean whats the difference then? – Sarah_Xx Nov 08 '18 at 20:31