What is the difference between the 2 parameter passes? [duplicate]

3

There is a parameter passing by value and by reference. I wanted examples to better understand the difference.

    
asked by anonymous 04.01.2017 / 19:07

2 answers

4

In this answer you find the detailed difference between "pass by value" and "pass by reference."

In summary and with examples in VisuAlg, the value tickets are made only copies of the values of the variables passed as parameter.

funcao soma (x,y: inteiro): inteiro
inicio
retorne x + y
fimfuncao

No programa principal deve haver os seguintes comandos:
n <- 4
m <- -9
res <- soma(n,m)
escreva(res)

Passing by reference modifies the variable you pass as a parameter.

procedimento soma (x,y: inteiro; var result: inteiro)
inicio
result <- x + y
fimprocedimento

No programa principal deve haver os seguintes comandos:
n <- 4
m <- -9
soma(n,m,res)
escreva(res)

Examples taken from: The VisuAlg Programming Language

    
04.01.2017 / 19:18
0

Passing value by reference becomes a feature that offers more dynamics, ie you are telling the compiler where the value is and not telling the value itself, referring to a variable eg would be where it is stored in memory. The opposite would be the parameter-by-value passing, where you simply say that 'variable x equals 10'.

    
04.01.2017 / 19:16