ProtoLang.net

Reference: End Function

☰ Hide Sidebar
TOKEN END FUNCTION
ALIAS None

The End Function token marks the end of a Function definition. Every function that begins with the Function token must be closed with End Function. This delimiter tells the parser where the function's code block ends and the main program resumes.

Syntax

The short syntax structure uses the words END FUNCTION:

FUNCTION NAME ( PARAMETERS )
    STATEMENTS
END FUNCTION

The long syntax structure uses the words END FUNCTION:

FUNCTION NAME GROUP PARAMETERS END GROUP
    STATEMENTS
END FUNCTION

How End Function Works

End Function the serves closing boundary for function definitions:

  1. Marks the end of the function's code block
  2. Completes the function definition started by FUNCTION
  3. Returns control to the main program or calling function
  4. Is required for every FUNCTION declaration

Example: Basic Function Structure

FUNCTION greet ( )
    PRINTLN "Hello!"
    RETURN 0
END FUNCTION

greet ( )
// Displays: Hello!

Basic End Function Usage

Example: Simple Function

FUNCTION add ( INT a , INT b )
    NEW INT sum = a + b
    RETURN sum
END FUNCTION

NEW INT result = add ( 5 , 3 )
PRINTLN result // Displays: 8

Example: Function with No Parameters

FUNCTION sayHello ( )
    PRINTLN "Hello, World!"
    RETURN 0
END FUNCTION

sayHello ( )
// Displays: Hello, World!

Example: Function Returning String

FUNCTION getGreeting ( STR name )
    NEW STR message = "Hello, " .. name
    RETURN message
END FUNCTION

NEW STR greeting = getGreeting ( "Alice" )
PRINTLN greeting // Displays: Hello, Alice!

End Function with Different Function Types

Example: Integer Function

FUNCTION multiply ( INT x , INT y )
    NEW INT product = x * y
    RETURN product
END FUNCTION

NEW INT answer = multiply ( 6 , 7 )
PRINTLN answer // Displays: 42

Example: Float Function

FUNCTION calculateArea ( FLOAT width , FLOAT height )
    NEW FLOAT area = width * height
    RETURN area
END FUNCTION

NEW FLOAT roomArea = calculateArea ( 10.5 , 8.0 )
PRINTLN roomArea // Displays: 84.0

Example: Boolean Function

FUNCTION isEven ( INT number )
    NEW BOOL result = ( number % 2 ) == 0
    RETURN result
END FUNCTION

NEW BOOL check = isEven ( 4 )
PRINTLN check // Displays: TRUE

End Function with Conditionals

Example: Function with If Statement

FUNCTION getSign ( INT n )
    IF n > 0
        RETURN "positive"
    ELIF n < 0
        RETURN "negative"
    ELSE
        RETURN "zero"
    END IF
END FUNCTION

PRINTLN getSign ( 10 ) // Displays: positive
PRINTLN getSign ( -5 ) // Displays: negative
PRINTLN getSign ( 0 ) // Displays: zero

Example: Function with Nested Conditionals

FUNCTION getGrade ( INT score )
    NEW STR grade = ""
    IF score >= 90
        SET grade = "A"
    ELIF score >= 80
        SET grade = "B"
    ELIF score >= 70
        SET grade = "C"
    ELIF score >= 60
        SET grade = "D"
    ELSE
        SET grade = "F"
    END IF
    RETURN grade
END FUNCTION

NEW STR letterGrade = getGrade ( 85 )
PRINTLN letterGrade // Displays: B

End Function with Loops

Example: Function with While Loop

FUNCTION factorial ( INT n )
    NEW INT result = 1
    NEW INT i = 1
    WHILE i <= n
        SET result = result * i
        SET i = i + 1
    END WHILE
    RETURN result
END FUNCTION

NEW INT fact5 = factorial ( 5 )
PRINTLN "5! = " .. fact5 // Displays: 5! = 120

Example: Function with Loop and Conditional

FUNCTION sumEvens ( INT start , INT end )
    NEW INT sum = 0
    NEW INT i = start
    WHILE i <= end
        IF i % 2 == 0
            SET sum = sum + i
        END IF
        SET i = i + 1
    END WHILE
    RETURN sum
END FUNCTION

NEW INT total = sumEvens ( 1 , 10 )
PRINTLN "Sum of evens: " .. total // Displays: Sum of evens: 30

Multiple Functions

Example: Two Functions

FUNCTION double ( INT n )
    RETURN n * 2
END FUNCTION

FUNCTION triple ( INT n )
    RETURN n * 3
END FUNCTION

NEW INT d = double ( 5 )
NEW INT t = triple ( 5 )
PRINTLN "Double: " .. d // Displays: Double: 10
PRINTLN "Triple: " .. t // Displays: Triple: 15

Example: Multiple Related Functions

FUNCTION add ( INT a , INT b )
    RETURN a + b
END FUNCTION

FUNCTION subtract ( INT a , INT b )
    RETURN a - b
END FUNCTION

FUNCTION multiply ( INT a , INT b )
    RETURN a * b
END FUNCTION

FUNCTION divide ( INT a , INT b )
    IF b == 0
        RETURN 0
    END IF
    RETURN a / b
END FUNCTION

PRINTLN add ( 10 , 5 ) // Displays: 15
PRINTLN subtract ( 10 , 5 ) // Displays: 5
PRINTLN multiply ( 10 , 5 ) // Displays: 50
PRINTLN divide ( 10 , 5 ) // Displays: 2

Functions Calling Other Functions

Example: Helper Functions

FUNCTION square ( INT n )
    RETURN n * n
END FUNCTION

FUNCTION sumOfSquares ( INT a , INT b )
    NEW INT sqA = square ( a )
    NEW INT sqB = square ( b )
    RETURN sqA + sqB
END FUNCTION

NEW INT result = sumOfSquares ( 3 , 4 )
PRINTLN result // Displays: 25

Example: Temperature Conversion Functions

FUNCTION celsiusToFahrenheit ( FLOAT celsius )
    NEW FLOAT fahrenheit = 0.0
    SET fahrenheit = ( celsius * 9.0 / 5.0 ) + 32.0
    RETURN fahrenheit
END FUNCTION

FUNCTION fahrenheitToCelsius ( FLOAT fahrenheit )
    NEW FLOAT celsius = 0.0
    SET celsius = ( fahrenheit - 32.0 ) * 5.0 / 9.0
    RETURN celsius
END FUNCTION

NEW FLOAT tempC = 25.0
NEW FLOAT tempF = 77.0
PRINTLN "Temperature Conversions:"
PRINTLN tempC .. " C = " .. celsiusToFahrenheit ( tempC ) .. " F"
PRINTLN tempF .. " F = " .. fahrenheitToCelsius ( tempF ) .. " C"
// Displays: Temperature Conversions:
// Displays: 25.0 C = 77.0 F
// Displays: 77.0 F = 25.0 C

Proper Function Boundaries

Example: Code After Function Definition

FUNCTION getValue ( )
    NEW INT x = 10
    RETURN x
END FUNCTION

// Main program continues here after END FUNCTION
NEW INT result = getValue ( )
PRINTLN result // Displays: 10

Example: Multiple Functions with Main Code

FUNCTION greet ( STR name )
    RETURN "Hello, " .. name
END FUNCTION

FUNCTION farewell ( STR name )
    RETURN "Goodbye, " .. name
END FUNCTION

// Main program
NEW STR greeting = greet ( "Alice" )
NEW STR goodbye = farewell ( "Alice" )
PRINTLN greeting // Displays: Hello, Alice
PRINTLN goodbye // Displays: Goodbye, Alice

Indentation and Structure

While ProtoLang is flexible with whitespace, proper indentation makes function boundaries clear.

Example: Well-Structured Function

FUNCTION calculate ( INT x , INT y )
    NEW INT sum = x + y
    NEW INT product = x * y
    IF sum > product
        RETURN sum
    ELSE
        RETURN product
    END IF
END FUNCTION

NEW INT result = calculate ( 5 , 3 )
PRINTLN result // Displays: 15

Example: Nested Structures

FUNCTION processNumbers ( INT start , INT end )
    NEW INT total = 0
    NEW INT i = start
    WHILE i <= end
        IF i % 2 == 0
            SET total = total + i
        END IF
        SET i = i + 1
    END WHILE
    RETURN total
END FUNCTION

NEW INT evenSum = processNumbers ( 1 , 10 )
PRINTLN evenSum // Displays: 30

Practical Applications

Example: Validation Function

FUNCTION isValidAge ( INT age )
    IF age < 0
        RETURN FALSE
    END IF
    IF age > 120
        RETURN FALSE
    END IF
    RETURN TRUE
END FUNCTION

NEW INT age = 25
IF isValidAge ( age )
    PRINTLN "Valid age: " .. age
ELSE
    PRINTLN "Invalid age"
END IF
// Displays: Valid age: 25

Example: String Processing

FUNCTION repeatString ( STR inputString , INT repeatTimes )
    NEW STR result = ""
    NEW INT i = 1
    WHILE i <= repeatTimes
        SET result = result .. inputString
        SET i = i + 1
    END WHILE
    RETURN result
END FUNCTION

NEW STR repeated = repeatString ( "Ha" , 3 )
PRINTLN repeated // Displays: HaHaHa

Example: Mathematical Calculation

FUNCTION power ( INT base , INT exponent )
    NEW INT result = 1
    NEW INT i = 1
    WHILE i <= exponent
        SET result = result * base
        SET i = i + 1
    END WHILE
    RETURN result
END FUNCTION

NEW INT value = power ( 2 , 3 )
PRINTLN "2^3 = " .. value // Displays: 2^3 = 8

Example: Range Checking

FUNCTION inRange ( INT value , INT min , INT max )
    IF value < min
        RETURN FALSE
    END IF
    IF value > max
        RETURN FALSE
    END IF
    RETURN TRUE
END FUNCTION

NEW INT score = 75
IF inRange ( score , 0 , 100 )
    PRINTLN "Score in valid range"
ELSE
    PRINTLN "Score out of range"
END IF
// Displays: Score in valid range

End Function with All Statement Types

Example: Function with Variable Declarations

FUNCTION calculateTotal ( FLOAT price , FLOAT quantity )
    NEW FLOAT subtotal = price * quantity
    NEW FLOAT taxRate = 0.08
    NEW FLOAT tax = subtotal * taxRate
    NEW FLOAT total = subtotal + tax
    RETURN total
END FUNCTION

NEW FLOAT finalPrice = calculateTotal ( 10.0 , 5.0 )
PRINTLN "Total: $" .. finalPrice // Displays: Total: $54.0

Example: Function with Multiple Operations

FUNCTION processData ( INT value )
    NEW INT doubled = value * 2
    NEW INT increased = doubled + 10
    NEW INT result = 0
    
    IF increased > 50
        SET result = increased / 2
    ELSE
        SET result = increased
    END IF
    
    RETURN result
END FUNCTION

NEW INT output = processData ( 20 )
PRINTLN output // Displays: 25

Important Notes

  • Every FUNCTION must have a matching END FUNCTION.
  • END FUNCTION marks the end of the function's code block.
  • Code written after END FUNCTION is not part of the function.
  • END FUNCTION is required even if the function has a Return statement that exits early.
  • Multiple functions can be defined, each with its own END FUNCTION.
  • END FUNCTION returns control to the calling code or main program.
  • Proper indentation helps visualize where END FUNCTION should be placed.
  • END FUNCTION cannot appear without a preceding FUNCTION declaration.
  • All code between FUNCTION and END FUNCTION is part of the function body.

Common Errors

Error: Missing END FUNCTION

FUNCTION add ( INT a , INT b )
    NEW INT sum = a + b
    RETURN sum
// Syntax error! Missing END FUNCTION

NEW INT result = add ( 5 , 3 ) // This won't work

// Correct:
FUNCTION add ( INT a , INT b )
    NEW INT sum = a + b
    RETURN sum
END FUNCTION

NEW INT result = add ( 5 , 3 )

Error: END FUNCTION Without FUNCTION

NEW INT x = 10
PRINTLN x
END FUNCTION // Syntax error! No matching FUNCTION

// Correct: Only use END FUNCTION after FUNCTION
FUNCTION getValue ( )
    NEW INT x = 10
    RETURN x
END FUNCTION

Error: Code Between Functions Not Closed

FUNCTION first ( )
    RETURN 1
// Missing END FUNCTION here!

FUNCTION second ( ) // Syntax error! Previous function not closed
    RETURN 2
END FUNCTION

// Correct:
FUNCTION first ( )
    RETURN 1
END FUNCTION

FUNCTION second ( )
    RETURN 2
END FUNCTION

Error: Misplaced END FUNCTION

FUNCTION calculate ( INT x )
    IF x > 0
        RETURN x * 2
    END FUNCTION // Syntax error! END FUNCTION in wrong place
    ELSE
        RETURN 0
    END IF
END FUNCTION

// Correct: END FUNCTION at the end of entire function
FUNCTION calculate ( INT x )
    IF x > 0
        RETURN x * 2
    ELSE
        RETURN 0
    END IF
END FUNCTION

Error: Extra END FUNCTION

FUNCTION getValue ( )
    NEW INT x = 10
    RETURN x
END FUNCTION
END FUNCTION // Syntax error! Extra END FUNCTION

// Correct: One END FUNCTION per FUNCTION
FUNCTION getValue ( )
    NEW INT x = 10
    RETURN x
END FUNCTION

Error: Nested Function Definitions

FUNCTION outer ( )
    FUNCTION inner ( ) // Syntax error! Cannot define function inside function
        RETURN 10
    END FUNCTION
    RETURN inner ( )
END FUNCTION

// Correct: Define functions separately
FUNCTION inner ( )
    RETURN 10
END FUNCTION

FUNCTION outer ( )
    NEW INT value = inner ( )
    RETURN value
END FUNCTION

Error: Using Function Name Before END FUNCTION

FUNCTION getValue ( )
    NEW INT x = 10
    RETURN x

NEW INT result = getValue ( ) // Syntax error! Still inside function
END FUNCTION

// Correct: Call function after it's fully defined
FUNCTION getValue ( )
    NEW INT x = 10
    RETURN x
END FUNCTION

NEW INT result = getValue ( )

Error: Wrong Delimiter

FUNCTION add ( INT a , INT b )
    RETURN a + b
END // Syntax error! Use END FUNCTION, not just END

// Correct:
FUNCTION add ( INT a , INT b )
    RETURN a + b
END FUNCTION

Error: Confusing with END IF or END WHILE

FUNCTION process ( INT n )
    IF n > 0
        RETURN n * 2
    END FUNCTION // Syntax error! Should be END IF
END IF // Syntax error! Extra END IF now

// Correct: Use proper delimiters
FUNCTION process ( INT n )
    IF n > 0
        RETURN n * 2
    END IF
    RETURN 0
END FUNCTION

Error: Missing Statements Before END FUNCTION

FUNCTION getValue ( )
END FUNCTION // Runtime error! No RETURN statement

// Correct: Include RETURN statement
FUNCTION getValue ( )
    RETURN 0
END FUNCTION

Error: Main Code Inside Function

FUNCTION calculate ( INT x )
    NEW INT result = x * 2
    PRINTLN result // This is part of the function
    RETURN result
END FUNCTION

// Any code here is OUTSIDE the function
PRINTLN "Starting" // This is NOT part of the function