0
About functions and procedures, I have a big doubt in this matter.
I know that a function will always return data and that procedure has no return, but every function has to fill in all parameters? I wonder why my code is not working, the sum of the numbers always comes to zero.
What’s the matter?
programa
{
funcao inicio(){
inteiro resultado = 0, n1=0, n2=0
escreve()
escreva(somar(n1,n2))
}
funcao escreve(){
inteiro n1, n2
escreva("Digite n1 ")
leia(n1)
escreva("Digite n2 ")
leia(n2)
}
funcao inteiro somar(inteiro a, inteiro b){
retorne a + b
}
}
Well, your job is
escreve
type reads and forgets. In function you declare two variables:n1
andn2
, with scope restricted to the function, then you read values for each of these variables, but there closes the function, ie read values are lost. Note that in its functioninicio
you declare the variablesn1
andn2
with initial value 0 and therefore the function callsomar(n1,n2)
return 0, regardless of what was read inescreve
. Study on the scope of variables.– anonimo