About passing by reference and by value

Asked

Viewed 55 times

-1

I can’t understand when to use one or the other. in this code, I used both forms and saw no change at the end. What’s the difference and when to use one or the other

Create a procedure that receives two values per reference and sort them in ascending order. Create a main algorithm to call the procedure and display values after ordering.

Algoritmo "procedimento"

Var
 x, y: inteiro

   Procedimento crescente(var A,B: inteiro)
   var
   cres: inteiro
   inicio

   Se (a < b) entao
         para cres de a ate b faca
            Escreva(cres)
            cres <- cres + 1
         fimpara
      Senao
        para cres de b ate a faca
             Escreva(cres)
             cres <- cres + 1
        fimpara
      Fimse

   FimProcedimento

Inicio

  x <- 10
  y <- 20


  crescente(x, y)

Fimalgoritmo

1 answer

0

I made a little adjustments to your program. Don’t be sad with me! : )

I tried to explain as best I could!

Algorithm "procedure"

Inicio

    Var x, y: inteiro

    x <- 10 
    y <- 20

    crescente(x, y) 

    escreva(x)
    escreva(y)

Fim 

/* 
  Comentario:
    Neste procedimento o primeiro parâmetro A é passagem por referência, porque precede de var, 
                           então o valor de A será alterado mudando o valor de x.
                       o segundo parâmetro B é passagem por valor, 
                           então o valor de B será alterado SOMENTE dentro do procedimento e não vai alterar o valor de y.
*/ 
Procedimento crescente(var A, B: inteiro) 

  inicio
        Se (A < B) entao 
            para A ate B faca 
                Escreva(A) 
                A <- A + 1 
            fim-para
        Senao
             para B ate A faca 
                Escreva(B) 
                B <- B + 1 
             fim-para
        fim-se
   fim

Fim-Procedimento

Endpoint

Let’s run your program:

X = 10 (Initial value) Y = 20 (Initial value)

made the procedure call: increasing(10,20)

running within the growing procedure:

A = 10 B = 20

As 10 is less than 20 will enter the first condition (If (A < B)). Then it will perform the repeat loop (for) will run from 10 to 20 and ends the repeat structure.

A = 10 displays 10 A = 11 displays 11 ... A=20

out of the growing procedure.

I added two commands write to x and y after the growing procedure.

displays 20 (x end value) displays 20 (y end value)

Good programming practice is to make a good indentation of your code to make clear the understanding of the code and helps a lot in maintenance (possible changes that need to be made).

[visualg]

  • Thank you, it’s a bit confusing for me. Actually a lot. I will run this code, let’s say if I take the var, to see what happens

  • Not at all! If you take the VAR the parameter becomes by value. The VAR that defines parameter by reference. In the beginning it is so, it has to persist, because with the practice it learns.

Browser other questions tagged

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