-1

As you can see below, my function doesn't return the right value in int main() but it does in the function itself. I'm a newbie to c++, can anyone explain to me why or what's the problem? Thanks!

#include <iostream>
using namespace std;

int a[100],n;

void citire(int n)
{
    int a[100];
    for(int i = 0 ; i < n ; i ++) {  // the for loop
        cin >> a[i];  // entering the numbers for each
    }
    cout << a[5] << endl; // returns the right number
}

int main()
{
    cout << "n= "; cin >> n; // how many numbers should the vector have.
    citire(n); // me calling the function
    cout << a[5]; // returns 0
}

Myon
  • 937
  • 13
  • 23
  • The array `a[]` is redeclared in function `citire`. Just remove it. – Damien Nov 10 '20 at 12:36
  • It happends because your function uses array, defined in your function scope. – Denis Sablukov Nov 10 '20 at 12:37
  • 1
    Please go to your text-books (or get [some good ones](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) if you don't have any yet) and read about *scope*. – Some programmer dude Nov 10 '20 at 12:39
  • 5
    you are confusing "return" and "print on the screen". Those are two completely different things. `// returns the right number` --> no the function does not return anything its return type is `void` – 463035818_is_not_an_ai Nov 10 '20 at 12:39
  • Your void function, by definition, doesn't return anything. That's what the `void` in `void citire(int n)` means. – Pete Becker Nov 10 '20 at 13:55

2 Answers2

0

Variable shadowing, where variables in different scopes have the same name. int a[100] inside the function citire is allocated on the stack will not persist when it falls out of scope at the end of the function, it's a different array to the global int a[100].

Colin
  • 3,394
  • 1
  • 21
  • 29
0

You have two different variables, a local variable and a global variable, but as you have given both of them the same name, you don't see that difference. Let me show you in a clearer way what you have programmed:

#include <iostream>
using namespace std;

int global_a[100],n;

void citire(int n)
{
int local_a[100];
for(int i = 0 ; i < n ; i ++) {  // the for loop
    cin >> local_a[i];  // entering the numbers for each
}
cout << local_a[5] << endl; // returns the right number
}

int main()
{
cout << "n= "; cin >> n; // how many numbers should the vector have.
citire(n); // me calling the function
cout << global_a[5]; // returns 0
}

Now, you tell me, where did you store any variable in the array global_a?

In order to avoid this, you might do the following:

#include <iostream>
using namespace std;

int global_a[100],n;

void citire(int n)
{
// int a[100]; don't declare a local variable, so while referring to "a",
//             the global variable will be used:
for(int i = 0 ; i < n ; i ++) {  // the for loop
    cin >> global_a[i];  // entering the numbers for each
}
cout << global_a[5] << endl; // returns the right number
}

int main()
{
cout << "n= "; cin >> n; // how many numbers should the vector have.
citire(n); // me calling the function
cout << global_a[5]; // returns 0
}

For your information, the prefixes "local_" and "global_" are just there for clarification purposes. In the last example, you might just write "a" instead of "global_a", the result will be the same.

Dominique
  • 16,450
  • 15
  • 56
  • 112