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:
Function Categories
Section titled “Function Categories”Math Functions
Section titled “Math Functions”Functions for mathematical operations including rounding, absolute values, square roots, and random number generation.
See the Math Functions page for details.
String Functions
Section titled “String Functions”Functions for manipulating and analyzing text strings, including case conversion, substring operations, and splitting.
See the String Functions page for details.
Array Functions
Section titled “Array Functions”Functions for working with arrays, including adding, removing, and searching for elements.
See the Array Functions page for details.
Type Checking
Section titled “Type Checking”Functions to check the type of a value at runtime.
See the Type Checking page for details.
Function Calling Syntax
Section titled “Function Calling Syntax”Common Pseudocode supports multiple syntax styles for calling functions:
Standard Syntax
Section titled “Standard Syntax”result = functionName(arg1, arg2)Natural Language Syntax
Section titled “Natural Language Syntax”result = run functionName with arg1 and arg2Operator-style Syntax
Section titled “Operator-style Syntax”For some functions, you can use natural language operators:
squareRoot = sqrt of 16absolute = abs of -5textLength = length of "hello"Function Composition
Section titled “Function Composition”You can combine functions to perform complex operations:
// Nested function callsresult = round(sqrt of 16) // 4
// Using results in expressionstext = " hello "trimmed = trim(text to uppercase) // "HELLO"
// Chaining with variablesnumbers = [3.7, 2.1, 8.9, 4.2]sum = 0for each num in numbers sum = sum + round(num)output "Sum of rounded numbers: {sum}" // 19Common Patterns
Section titled “Common Patterns”Input Validation
Section titled “Input Validation”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"Array Processing
Section titled “Array Processing”scores = [85, 92, 78, 95, 88]
// Calculate averagesum = 0for each score in scores sum = sum + scoreaverage = sum / length of scoresoutput "Average: {average}"
// Find highest scorehighest = scores[0]for each score in scores highest = max(highest, score)output "Highest score: {highest}"String Formatting
Section titled “String Formatting”firstName = "john"lastName = "doe"
// Convert to title casefirstName = 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"Random Selection
Section titled “Random Selection”options = ["Rock", "Paper", "Scissors"]randomIndex = random integer from 0 to length of options - 1choice = 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 togetherradius = 5.7area = calculateCircleArea(radius)output formatArea(area) // "Area: 102 sq units"Function Return Values
Section titled “Function Return Values”Most built-in functions return values that can be:
Stored in Variables
Section titled “Stored in Variables”result = sqrt of 16output resultUsed Directly in Expressions
Section titled “Used Directly in Expressions”if length of name > 0 output "Valid name"Passed to Other Functions
Section titled “Passed to Other Functions”rounded = round(sqrt of 20) // round(4.472...) = 4Used in Conditions
Section titled “Used in Conditions”if is number(userInput) // Process numeric inputError Handling with Functions
Section titled “Error Handling with Functions”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 returnsindex = find "xyz" in textif index equals -1 output "Not found"else output "Found at position {index}"Best Practices
Section titled “Best Practices”1. Use Descriptive Variable Names
Section titled “1. Use Descriptive Variable Names”// GooduserAge = convert ageInput to numbervalidEmail = email contains "@"
// Avoidx = convert y to numberflag = email contains "@"2. Combine Related Operations
Section titled “2. Combine Related Operations”// Instead of multiple conversionscleanedInput = trim(userInput to lowercase)
// Break into steps for complex operationsrawInput = userInputtrimmed = trim(rawInput)cleaned = trimmed to lowercase3. Check Types Before Operations
Section titled “3. Check Types Before Operations”if is array(data) AND length of data > 0 firstElement = data[0] output firstElement4. Handle Edge Cases
Section titled “4. Handle Edge Cases”function safeDivide takes in numerator and denominator if denominator equals 0 return 0 return round(numerator / denominator)User-Defined Functions
Section titled “User-Defined Functions”You can create your own functions using the function keyword:
function add takes in num1 and num2 return num1 + num2
result = add(5, 3) // 8result = run add with 5 and 3 // 8See the Creating Functions page for a comprehensive guide on creating your own functions, including examples, best practices, and advanced techniques.