🔧 Functions in iPseudo

Master creating reusable, modular code with functions!

🎯 What are Functions?

Functions are reusable blocks of code that:

  • Perform specific tasks
  • Can accept input (parameters)
  • Can return output (return values)
  • Make code organized and maintainable

📐 Function Syntax

pseudocode
Function functionName(parameter1, parameter2)
    # Function body
    # Code to execute
    Return value  # Optional
Endfunction

🔷 Simple Function

Algorithm SimpleFunctionExample

Function sayHello()
    Print "Hello from the function!"
    Print "Functions are awesome!"
Endfunction

# Main program
Print "Starting program"
sayHello()  # Call the function
Print "Program continues"

Endalgorithm

🎁 Function with Return Value

Algorithm ReturnValueExample

Function double(number)
    var result = number * 2
    Return result
Endfunction

# Main program
var value = 5
var doubled = double(value)

Print "Original:", value      # 5
Print "Doubled:", doubled      # 10

Endalgorithm

🎯 Complete Example: Grade System

pseudocode
Algorithm GradeSystem

Function getGrade(score)
    If score >= 90 Then
        Return "A"
    Elseif score >= 80 Then
        Return "B"
    Elseif score >= 70 Then
        Return "C"
    Elseif score >= 60 Then
        Return "D"
    Else
        Return "F"
    Endif
Endfunction

# Main program
var mathScore = Input "Math score:"
var mathGrade = getGrade(mathScore)

Print "Math:", mathScore, "→", mathGrade

Endalgorithm

💡 Best Practices

  • Single Responsibility: Each function should do ONE thing well
  • Descriptive Names: Use clear function names like calculateTotal
  • Keep Functions Short: Easier to understand and maintain
  • Document Your Functions: Add comments explaining parameters and return values