Skip to content
EspañolWorkspace →

Functions in PSeInt: examples that return a value

8 examples

How it works

A function is a SubProceso that computes something and returns the result, so you can use it inside an expression just like Raiz or Abs: Escribir Promedio(a, b) or Si EsPrimo(n) Entonces.

In PSeInt a function is declared with the variable it will return: Funcion r <- Doble(n). Whatever r holds when FinFuncion is reached is the result. Codoritmo also accepts Retornar to return a value at any point.

Functions make a program easier to read and to test in parts: you can check that EsPrimo works on a few numbers before using it in a bigger problem.

Short example

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

functions.psc · 7 lines
Funcion r <- Promedio(a, b)    r <- (a + b) / 2FinFuncionAlgoritmo UseFunction    Escribir "Average of 14 and 17: ", Promedio(14, 17)FinAlgoritmo

What it prints:

Average of 14 and 17: 15.5
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.

  • Linear vs. Binary Search

    Counts the comparisons each search needs on the same sorted list.

    Intermediate
  • Factorial and Power

    Functions that compute a factorial and a power.

    Intermediate
  • Reusable Validation

    Validates the email and age on a sign-up form.

    Intermediate
  • Prime Numbers

    Finds every prime number up to a limit.

    Intermediate
  • GCD and LCM

    Computes the greatest common divisor and the least common multiple.

    Intermediate
  • Quicksort

    Picks a pivot, splits smaller from larger values and sorts each side recursively.

    Advanced
  • Tic-Tac-Toe

    The classic three-in-a-row game for two players.

    Advanced
  • Class Grade Book

    Records three grades per student and builds a class report.

    Advanced

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.