Scope of variables

Asked

Viewed 117 times

0

  1. Why we have 2 variables with the same name in the program in question?
  2. What value the program prints?
  3. What should we do to get the 15 printed?
  4. Considering the scope of variables, how do we classify the variable of line 4? And how do we classify the variable of line 8?

I have this code, and I need it to print the value 15, what should I do? Portugol Exercise.

programa
{

    inteiro valor = 15

    funcao inicio()
    {
        inteiro valor = 10

        escreva("valor=", valor)
    }
}
  • 1

    You who wrote this code or it is given in the exercise statement?

  • the whole exercise is like this:

    1. Why do we have 2 variables with the same name in the program in question? 2) What value does the program print? 3) What should we do to have the value 15 printed? 4) Thinking about the scope of variables, how do we classify the variable of line 4? And how do we classify the variable of line 8?

1 answer

0

By logic and taking advantage of what you are trying to do, I believe the solution is as follows:

inteiro valor = 15

programa
{
    funcao inicio()
    {
        escreva("valor=", valor)
    }
}

This variable valor only needs to be called once it is in the global variable situation.

Browser other questions tagged

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