Scope of variables

0
  
  • Why do we have 2 variables with the same name in the program in question?
  •   
  • What value does the program print?
  •   
  • What should we do to get the value 15 printed?
  •   
  • Thinking about variable scope, how do we classify the variable in row 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? Exercise of Portugol.

    programa
    {
    
        inteiro valor = 15
    
        funcao inicio()
        {
            inteiro valor = 10
    
            escreva("valor=", valor)
        }
    }
    
        
    asked by anonymous 30.05.2018 / 01:43

    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 when it is in the global variable situation.

        
    30.05.2018 / 02:12