Skip to content

Built-in Functions Overview

Common Pseudocode provides a rich set of built-in functions to perform common operations. These functions are organized into several categories:

Functions for mathematical operations including rounding, absolute values, square roots, and random number generation.

See the Math Functions page for details.

Functions for manipulating and analyzing text strings, including case conversion, substring operations, and splitting.

See the String Functions page for details.

Functions for working with arrays, including adding, removing, and searching for elements.

See the Array Functions page for details.

Functions to check the type of a value at runtime.

See the Type Checking page for details.

Common Pseudocode supports multiple syntax styles for calling functions:

result = functionName(arg1, arg2)
result = run functionName with arg1 and arg2

For some functions, you can use natural language operators:

squareRoot = sqrt of 16
absolute = abs of -5
textLength = length of "hello"

You can combine functions to perform complex operations:

// Nested function calls
result = round(sqrt of 16) // 4
// Using results in expressions
text = " hello "
trimmed = trim(text to uppercase) // "HELLO"
// Chaining with variables
numbers = [3.7, 2.1, 8.9, 4.2]
sum = 0
for each num in numbers
sum = sum + round(num)
output "Sum of rounded numbers: {sum}" // 19
output "Enter your age: "
input ageInput
if is number(convert ageInput to number)
age = convert ageInput to number
if age >= 0 AND age <= 120
output "Valid age: {age}"
else
output "Age out of range"
else
output "Not a valid number"
scores = [85, 92, 78, 95, 88]
// Calculate average
sum = 0
for each score in scores
sum = sum + score
average = sum / length of scores
output "Average: {average}"
// Find highest score
highest = scores[0]
for each score in scores
highest = max(highest, score)
output "Highest score: {highest}"
firstName = "john"
lastName = "doe"
// Convert to title case
firstName = substring of (firstName to uppercase) from 0 to 1
concat substring of firstName from 1 to length of firstName
lastName = substring of (lastName to uppercase) from 0 to 1
concat substring of lastName from 1 to length of lastName
fullName = join [firstName, lastName] with " "
output fullName // "John Doe"
options = ["Rock", "Paper", "Scissors"]
randomIndex = random integer from 0 to length of options - 1
choice = options[randomIndex]
output "Computer chose: {choice}"

Combining Built-in and User-Defined Functions

Section titled “Combining Built-in and User-Defined Functions”

Built-in functions work seamlessly with your custom functions:

function calculateCircleArea takes in radius
constant PI = 3.14159
return PI * (radius ^ 2)
function formatArea takes in area
rounded = round(area)
return "Area: " convert rounded to string " sq units"
// Using both together
radius = 5.7
area = calculateCircleArea(radius)
output formatArea(area) // "Area: 102 sq units"

Most built-in functions return values that can be:

result = sqrt of 16
output result
if length of name > 0
output "Valid name"
rounded = round(sqrt of 20) // round(4.472...) = 4
if is number(userInput)
// Process numeric input

Some functions may fail or return special values:

try
// Division might fail
result = 10 / denominator
output round(result)
catch error
output "Cannot calculate result"
// Checking for null returns
index = find "xyz" in text
if index equals -1
output "Not found"
else
output "Found at position {index}"
// Good
userAge = convert ageInput to number
validEmail = email contains "@"
// Avoid
x = convert y to number
flag = email contains "@"
// Instead of multiple conversions
cleanedInput = trim(userInput to lowercase)
// Break into steps for complex operations
rawInput = userInput
trimmed = trim(rawInput)
cleaned = trimmed to lowercase
if is array(data) AND length of data > 0
firstElement = data[0]
output firstElement
function safeDivide takes in numerator and denominator
if denominator equals 0
return 0
return round(numerator / denominator)

You can create your own functions using the function keyword:

function add takes in num1 and num2
return num1 + num2
result = add(5, 3) // 8
result = run add with 5 and 3 // 8

See the Creating Functions page for a comprehensive guide on creating your own functions, including examples, best practices, and advanced techniques.