Skip to content
EspañolWorkspace →

PSeInt Por Referencia vs Por Valor (pass by reference)

1 example

How it works

When you call a SubProceso, each parameter arrives Por Valor (by value) or Por Referencia (by reference). By value gets a copy: whatever the SubProceso does to it doesn't change the original variable. That's what happens if you don't say anything.

By reference gets the variable itself: if the SubProceso changes it, the change shows outside. That's what you need to swap two values, sort, or give back more than one result.

Arrays are passed by reference even if you don't write it, which is why a SubProceso can sort an array in place.

Short example

The least you need to see it work. Copy it or open it in the editor.

pass-by-reference.psc · 14 lines
SubProceso Intercambiar(a Por Referencia, b Por Referencia)    Definir aux Como Entero    aux <- a    a <- b    b <- auxFinSubProcesoAlgoritmo ReferenceTest    Definir p, q Como Entero    p <- 1    q <- 2    Intercambiar(p, q)    Escribir "p = ", p, ", q = ", qFinAlgoritmo

What it prints:

p = 2, q = 1
Open it in Codoritmo

Opens in the browser editor, nothing to install.

Solved exercises

Each exercise comes with the full code, its flowchart and a sample run.

In the language reference

The instructions involved, explained one by one.

Other concepts

Codoritmo is an independent, open-source project inspired by PSeInt and compatible with its syntax. It is not affiliated with the PSeInt project. Official PSeInt website.