What is the difference between the 2 parameter passages?

Asked

Viewed 612 times

3

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

2 answers

4


In that reply you find the detailed difference between "passing by value" and "passing by reference".

In summary and with examples in Visualg, the passage by value is 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)

The passage by reference modifies the variable you pass as 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

0

Passing value by reference becomes a more dynamic feature, i.e., you are telling the compiler where the value is and not stating the value itself, referring to a variable for example, is where it is stored in memory. The opposite would be the passing of parameter by value, where you simply say that 'variable x equals 10'.

Browser other questions tagged

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