When I create a function in C++ do the variables within it stay in memory?

Asked

Viewed 216 times

2

When I was studying pseudocode, I learned that when you call a function and create a variable, it only "exists" when I call that function, for example.

funcao teste():
      x = 10
      retorna x

In case, when I called my test function, it would create the variable x, and after she returned he would erase.

I’m studying C++ and tried to do this function to generate random numbers.

int gerar_numeros() {
    srand(time(NULL));

    int x = rand() % 100;

    return x;
}

and to assign this part in the function to the vector main().

for (int c = 0; c <= 10; c++)
    {
        vetor[c] = gerar_numeros();
    }

But the problem is he only changed the numbers when I ran the script again, for example, the first time the all values of the vector was 5, the second time 10 and so on, so I changed the logic to this one and it worked.

void gerar_numeros(int vetor[]) {
    srand(time(NULL));

    //int x = 1 + rand() % 100;

    for (int c = 0; c <= 10; c++)
    {
        vetor[c] = rand() % 100;
    }

In that logic from above is as if the variable x was stored in memory, and so always returns the same number.

1 answer

6


Variable is a more abstract concept. It is something that helps you understand the memorization of data in your code. What goes effectively in memory is not the variable. In the code through it you access a memory position where the data is. When using variable does not exist, only an address.

What you must be talking about is scope variable.

The first fault function creates a memory position that will be accessed by your code with the name of x and there will be the value 10, soon after this value is copied to another area because of return and the original value is discarded at the end of the function.

In the second code no variable is required, just return what you want. But if you will call it more than once you cannot call the srand() in it, this function initializes the random number generator, so every time is going to the beginning of the list.

One problem has nothing to do with the other. It is not variable scope problem.

You tried a random solution (I couldn’t resist) and got a solution to the problem, but probably not in the most appropriate way.

Since you’ve already initialized the random seed, you can just do this:

int gerar_numeros() { return rand() % 100; }

I put in the Github for future reference.

Note that the numbers are not exactly random, it is called pseudorandom. It serves for a lot, but not for everything. Even C++ already has a better solution than C, which was used.

  • Got it, the problem is that I was starting srand() every time I called the right function? Thanks!!

  • 2

    @Zaque Sim srand sets the starting point of random generation. If you always set the same starting point before generating a number then always generate the same number.

  • 1

    To be more complete, the problem is not so much that called srand() several times, because it is giving a seed of time(NULL), that changes. The problem is that called srand() several times very fast. The code is completing before time(NULL) change in value. If you run the original code to generate many more numbers (maybe 100000), it will result in many numbers of the same value followed by many numbers of another value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.