Functions are reusable blocks of code that perform a specific task and return a value. They help you avoid repeating code and make your programs more organized and easier to maintain.
Function FunctionName(parameters)
# Function code
Return result
Endfunction
var result = FunctionName(arguments)
Algorithm FunctionDemo
# Define the function
Function AddNumbers(a, b)
var sum = a + b
Return sum
Endfunction
# Use the function
var result1 = AddNumbers(5, 3)
var result2 = AddNumbers(10, 20)
Print "5 + 3 =", result1
Print "10 + 20 =", result2
Endalgorithm
Parameters are variables that receive values when the function is called.
Algorithm NoParamsExample
Function GetGreeting()
Return "Hello, World!"
Endfunction
var message = GetGreeting()
Print message
Endalgorithm
Algorithm OneParamExample
Function Square(number)
Return number * number
Endfunction
Print "Square of 5:", Square(5)
Print "Square of 12:", Square(12)
Endalgorithm
Algorithm MultipleParams
Function CalculateArea(length, width)
Return length * width
Endfunction
Function CalculateVolume(length, width, height)
Return length * width * height
Endfunction
var area = CalculateArea(5, 10)
var volume = CalculateVolume(5, 10, 3)
Print "Area:", area
Print "Volume:", volume
Endalgorithm
Functions can return different types of values:
Algorithm ReturnTypes
# Returns a number
Function GetAge()
Return 25
Endfunction
# Returns a string
Function GetName()
Return "Alice"
Endfunction
# Returns a boolean
Function IsAdult(age)
Return age >= 18
Endfunction
Print "Name:", GetName()
Print "Age:", GetAge()
Print "Is adult:", IsAdult(GetAge())
Endalgorithm
Algorithm TemperatureConverter
# Convert Celsius to Fahrenheit
Function CelsiusToFahrenheit(celsius)
Return celsius * 9 / 5 + 32
Endfunction
# Convert Fahrenheit to Celsius
Function FahrenheitToCelsius(fahrenheit)
Return (fahrenheit - 32) * 5 / 9
Endfunction
Print "=== Temperature Converter ==="
Print ""
var tempC = Input "Enter temperature in Celsius:"
var tempF = CelsiusToFahrenheit(tempC)
Print tempC, "°C =", tempF, "°F"
Print ""
var tempF2 = Input "Enter temperature in Fahrenheit:"
var tempC2 = FahrenheitToCelsius(tempF2)
Print tempF2, "°F =", tempC2, "°C"
Endalgorithm
Algorithm GradeCalculator
# Calculate average of three scores
Function CalculateAverage(score1, score2, score3)
var total = score1 + score2 + score3
Return total / 3
Endfunction
# Get letter grade from numeric score
Function GetLetterGrade(score)
If score >= 90 Then
Return "A"
Else If score >= 80 Then
Return "B"
Else If score >= 70 Then
Return "C"
Else If score >= 60 Then
Return "D"
Else
Return "F"
Endif
Endfunction
# Check if passing
Function IsPassing(score)
Return score >= 60
Endfunction
# Main program
Print "=== Grade Calculator ==="
Print ""
var test1 = Input "Test 1 score:"
var test2 = Input "Test 2 score:"
var test3 = Input "Test 3 score:"
var average = CalculateAverage(test1, test2, test3)
var grade = GetLetterGrade(average)
var passing = IsPassing(average)
Print ""
Print "=== RESULTS ==="
Print "Average score:", average
Print "Letter grade:", grade
Print "Passing:", passing
Endalgorithm
Algorithm MathUtilities
# Check if number is even
Function IsEven(number)
Return number mod 2 == 0
Endfunction
# Get absolute value
Function Abs(number)
If number < 0 Then
Return -number
Else
Return number
Endif
Endfunction
# Get maximum of two numbers
Function Max(a, b)
If a > b Then
Return a
Else
Return b
Endif
Endfunction
# Calculate factorial
Function Factorial(n)
var result = 1
For i = 1 To n
result = result * i
Endfor
Return result
Endfunction
# Test the functions
Print "Is 10 even?", IsEven(10)
Print "Abs(-5):", Abs(-5)
Print "Max(15, 23):", Max(15, 23)
Print "5! =", Factorial(5)
Endalgorithm
Functions can call other functions!
Algorithm NestedFunctions
Function Add(a, b)
Return a + b
Endfunction
Function Multiply(a, b)
Return a * b
Endfunction
# This function uses other functions
Function CalculateTotal(price, quantity, taxRate)
var subtotal = Multiply(price, quantity)
var tax = Multiply(subtotal, taxRate)
var total = Add(subtotal, tax)
Return total
Endfunction
var finalPrice = CalculateTotal(19.99, 3, 0.08)
Print "Total price:", finalPrice
Endalgorithm
â Wrong:
Function Add(a, b)
var sum = a + b
# Forgot to return!
Endfunction
â Correct:
Function Add(a, b)
var sum = a + b
Return sum
Endfunction
Function
, close with Endfunction
Return
to send a value back to the caller