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.
Funcion r <- Promedio(a, b) r <- (a + b) / 2FinFuncionAlgoritmo UseFunction Escribir "Average of 14 and 17: ", Promedio(14, 17)FinAlgoritmoWhat it prints:
Average of 14 and 17: 15.5Opens 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.
IntermediateFactorial and Power
Functions that compute a factorial and a power.
IntermediateReusable Validation
Validates the email and age on a sign-up form.
IntermediatePrime Numbers
Finds every prime number up to a limit.
IntermediateGCD and LCM
Computes the greatest common divisor and the least common multiple.
IntermediateQuicksort
Picks a pivot, splits smaller from larger values and sorts each side recursively.
AdvancedTic-Tac-Toe
The classic three-in-a-row game for two players.
AdvancedClass 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.