Skip to content
ExamplesEspañolWorkspace →

By Reference and by Value

Sorts three numbers by swapping them in subprocesses.

Level
Intermediate
Concepts:
Input & output
SubProcesos
Por Referencia
Si
Open in Codoritmo

Run, change and debug this example in your browser.

What you'll learn

With Por Referencia a SubProceso changes the original variables; with Por Valor it only changes a copy. One SubProceso can call another, as OrdenarTres calls Intercambiar.

Code

references.psc · 33 lines
SubProceso Intercambiar(a Por Referencia, b Por Referencia)    Definir temporal Como Real    temporal <- a    a <- b    b <- temporalFinSubProcesoSubProceso OrdenarTres(n1 Por Referencia, n2 Por Referencia, n3 Por Referencia)    Si n1 > n2 Entonces        Intercambiar(n1, n2)    FinSi    Si n2 > n3 Entonces        Intercambiar(n2, n3)    FinSi    Si n1 > n2 Entonces        Intercambiar(n1, n2)    FinSiFinSubProcesoSubProceso IntentarCambiar(n Por Valor)    // Solo cambia la copia local    n <- 0FinSubProcesoAlgoritmo PorReferencia    Definir a, b, c Como Real    Escribir "Escribe tres números:"    Leer a, b, c    OrdenarTres(a, b, c)    Escribir "Ordenados: ", a, " ", b, " ", c    IntentarCambiar(a)    Escribir "Por valor, a sigue valiendo ", aFinAlgoritmo

Flowchart

Each block in the flowchart matches one instruction in the code.

Algoritmo PorReferencia
Algoritmo PorReferenciaAlgoritmo PorReferenciaAlgoritmo PorReferenciaDefinir a, b, c Como RealDefinir a, b, c Como RealEscribir "Escribe tres números:"Escribir "Escribe tresnúmeros:"Leer a, b, cLeer a, b, cOrdenarTres(a, b, c)OrdenarTres(a, b, c)Escribir "Ordenados: ", a, " ", b, " ", cEscribir "Ordenados: ", a, "", b, " ", cIntentarCambiar(a)IntentarCambiar(a)Escribir "Por valor, a sigue valiendo ", aEscribir "Por valor, a siguevaliendo ", aFinAlgoritmoFinAlgoritmo
SubProceso Intercambiar
SubProceso IntercambiarSubProceso Intercambiar(a Por Referencia, b Por Referencia)SubProceso Intercambiar(a PorReferencia, b Por Referencia)Definir temporal Como RealDefinir temporal Como Realtemporal <- atemporal <- aa <- ba <- bb <- temporalb <- temporalFinSubProcesoFinSubProceso
SubProceso OrdenarTres
SubProceso OrdenarTresSíNoSíNoSíNoSubProceso OrdenarTres(n1 Por Referencia, n2 Por Referencia, n3 Por Referencia)SubProceso OrdenarTres(n1 PorReferencia, n2 Por Referencia, n3…Si n1 > n2 Entoncesn1 > n2Intercambiar(n1, n2)Intercambiar(n1, n2)Si n2 > n3 Entoncesn2 > n3Intercambiar(n2, n3)Intercambiar(n2, n3)Si n1 > n2 Entoncesn1 > n2Intercambiar(n1, n2)Intercambiar(n1, n2)FinSubProcesoFinSubProceso
SubProceso IntentarCambiar
SubProceso IntentarCambiarSubProceso IntentarCambiar(n)SubProceso IntentarCambiar(n)n <- 0n <- 0FinSubProcesoFinSubProceso

Sample run

This is what the program shows when it runs. The answers typed by the user are highlighted.

Escribe tres números:User answer: 9User answer: 2User answer: 5Ordenados: 2 5 9Por valor, a sigue valiendo 2

Challenge

Add a SubProceso Duplicar(n Por Referencia) and use it to double the largest of the three.

Show hint

After OrdenarTres, the largest number is always in c.

Open in Codoritmo
  • Towers of Hanoi

    Solves the classic tower puzzle step by step.

    Advanced
  • Bubble Sort

    Sorts random numbers by swapping neighbors, and stops as soon as they're in order.

    Intermediate
  • Selection Sort

    Finds the smallest of the remaining numbers and puts it in its place.

    Intermediate