ProtoLang.net

Reference: Parameter

☰ Hide Sidebar
TOKEN None
ALIAS None

A Parameter is a variable declaration in a function definition that specifies what values the function can receive when called. Each parameter must declare its Data Type and name, creating a local variable that exists only within the function's scope.

Parameter Structure

Parameters are declared inside the Group delimiters following a function name. Each parameter consists of:

  • A Data Type
  • A parameter name (following variable naming rules)

Single Parameter

A function with one parameter receives a single value when called.

Example: Single Integer Parameter

FUNCTION square ( INT n )
    NEW INT result = n * n
    RETURN result
END FUNCTION

NEW INT value = square ( 5 )
PRINTLN value // Displays "25"

Example: Single String Parameter

FUNCTION greet ( STR name )
    PRINTLN "Hello, " .. name .. "!"
END FUNCTION

greet ( "Alice" )
// Displays "Hello, Alice!"

Example: Single Float Parameter

FUNCTION double ( FLOAT value )
    NEW FLOAT result = value * 2.0
    RETURN result
END FUNCTION

NEW FLOAT doubled = double ( 3.5 )
PRINTLN doubled // Displays "7.0"

Example: Single Boolean Parameter

FUNCTION checkStatus ( BOOL isActive )
    IF isActive == TRUE
        PRINTLN "Status: Active"
    ELSE
        PRINTLN "Status: Inactive"
    END IF
END FUNCTION

checkStatus ( TRUE )
// Displays "Status: Active"

Multiple Parameters

When a function needs multiple values, parameters are separated by the Divider delimiter (comma). Each parameter must have its own data type declaration.

Example: Two Parameters

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: Three Parameters

FUNCTION calculateAverage ( FLOAT a , FLOAT b , FLOAT c )
    NEW FLOAT sum = a + b + c
    NEW FLOAT avg = sum / 3.0
    RETURN avg
END FUNCTION

NEW FLOAT average = calculateAverage ( 10.0 , 20.0 , 30.0 )
PRINTLN average // Displays "20.0"

Example: Four Parameters with Different Types

FUNCTION displayInfo ( STR name , INT age , FLOAT height , BOOL isStudent )
    PRINTLN "Name: " .. name
    PRINTLN "Age: " .. age
    PRINTLN "Height: " .. height
    PRINTLN "Student: " .. isStudent
END FUNCTION

displayInfo ( "Bob" , 25 , 5.9 , TRUE )

Example: Five Parameters

FUNCTION createRecord ( STR firstName , STR lastName , INT age , FLOAT salary , BOOL active )
    NEW STR fullName = firstName .. " " .. lastName
    PRINTLN "Employee: " .. fullName
    PRINTLN "Age: " .. age
    PRINTLN "Salary: $" .. salary
    PRINTLN "Active: " .. active
END FUNCTION

createRecord ( "John" , "Doe" , 30 , 50000.0 , TRUE )

No Parameters

Functions can be defined with no parameters, indicated by empty Group delimiters.

Example: Function with No Parameters

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

sayHello ( )
// Displays "Hello, World!"

Example: Returning a Value with No Parameters

FUNCTION getDefaultValue ( )
    RETURN 42
END FUNCTION

NEW INT value = getDefaultValue ( )
PRINTLN value // Displays "42"

Parameters vs Arguments

Parameters are the variable declarations in the function definition. Arguments are the actual values passed when calling the function.

Example: Parameters and Arguments

// Parameters: INT x and INT y (defined in function)
FUNCTION multiply ( INT x , INT y )
    RETURN x * y
END FUNCTION

// Arguments: 6 and 7 (passed to function)
NEW INT result = multiply ( 6 , 7 )
PRINTLN result // Displays "42"

Parameter Scope

Parameters are local variables that exist only within the function. They cannot be accessed outside the function.

Example: Parameter Scope

FUNCTION calculate ( INT value )
    NEW INT doubled = value * 2
    RETURN doubled
END FUNCTION

NEW INT result = calculate ( 10 )
PRINTLN result // Displays "20"
// PRINTLN value // Runtime error! value doesn't exist here

Example: Parameters Don't Affect External Variables

NEW INT original = 5

FUNCTION modifyValue ( INT num )
    SET num = num + 10
    PRINTLN "Inside function: " .. num
    RETURN num
END FUNCTION

NEW INT modified = modifyValue ( original )
PRINTLN "Original: " .. original // Displays "Original: 5"
PRINTLN "Modified: " .. modified // Displays "Modified: 15"

Parameter Order

Arguments are matched to parameters by position. The first argument is assigned to the first parameter, the second to the second, and so on.

Example: Order Matters

FUNCTION divide ( INT numerator , INT denominator )
    NEW INT result = numerator / denominator
    RETURN result
END FUNCTION

NEW INT answer1 = divide ( 10 , 2 )
PRINTLN answer1 // Displays "5" (10 / 2)

NEW INT answer2 = divide ( 2 , 10 )
PRINTLN answer2 // Displays "0" (2 / 10 = 0 in integer division)

Example: Descriptive Parameter Names

FUNCTION calculateDiscount ( FLOAT originalPrice , FLOAT discountPercent )
    NEW FLOAT discount = originalPrice * ( discountPercent / 100.0 )
    NEW FLOAT finalPrice = originalPrice - discount
    RETURN finalPrice
END FUNCTION

NEW FLOAT price = calculateDiscount ( 100.0 , 20.0 )
PRINTLN price // Displays "80.0"

Parameter Naming

Parameter names follow the same rules as variable names: they can contain letters (both uppercase and lowercase) and are case-sensitive.

Example: Case-Sensitive Parameters

FUNCTION example ( INT value , INT Value , INT VALUE )
    NEW INT sum = value + Value + VALUE
    RETURN sum
END FUNCTION

NEW INT total = example ( 1 , 2 , 3 )
PRINTLN total // Displays "6"

Using Parameters in Different Data Types

Example: Boolean Parameters

FUNCTION checkEligibility ( BOOL isAdult , BOOL hasLicense )
    IF isAdult && hasLicense
        RETURN TRUE
    ELSE
        RETURN FALSE
    END IF
END FUNCTION

NEW BOOL canDrive = checkEligibility ( TRUE , TRUE )
PRINTLN canDrive // Displays "TRUE"

Example: Integer Parameters

FUNCTION isInRange ( INT value , INT min , INT max )
    NEW BOOL inRange = ( value >= min ) && ( value <= max )
    RETURN inRange
END FUNCTION

IF isInRange ( 50 , 1 , 100 )
    PRINTLN "In range"
END IF
// Displays "In range"

Example: Float Parameters

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

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

Example: String Parameters

FUNCTION getFullName ( STR firstName , STR lastName )
    NEW STR fullName = firstName .. " " .. lastName
    RETURN fullName
END FUNCTION

NEW STR name = getFullName ( "John" , "Doe" )
PRINTLN name // Displays "John Doe"

Parameters in Complex Functions

Example: Parameters with 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"

Example: Parameters with Loops

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"

Practical Applications

Example: Temperature Conversion

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

NEW FLOAT tempF = celsiusToFahrenheit ( 25.0 )
PRINTLN tempF // Displays "77.0"

Example: Tax Calculator

FUNCTION calculateTax ( FLOAT amount , FLOAT rate )
    NEW FLOAT tax = amount * rate
    RETURN tax
END FUNCTION

FUNCTION getTotalWithTax ( FLOAT amount , FLOAT taxRate )
    NEW FLOAT tax = calculateTax ( amount , taxRate )
    NEW FLOAT total = amount + tax
    RETURN total
END FUNCTION

NEW FLOAT total = getTotalWithTax ( 100.0 , 0.08 )
PRINTLN "Total: $" .. total // Displays "Total: $108.0"

Example: String Formatter

FUNCTION formatMessage ( STR prefix , STR content , STR suffix )
    NEW STR message = prefix .. " " .. content .. " " .. suffix
    RETURN message
END FUNCTION

NEW STR output = formatMessage ( "[INFO]" , "Process completed" , "successfully" )
PRINTLN output // Displays "[INFO] Process completed successfully"

Important Notes

  • Each parameter must declare its Data Type.
  • Multiple parameters are separated by commas (Dividers).
  • Parameter names must be unique within the function.
  • Parameters are local variables that only exist inside the function.
  • Arguments must match parameters in number, order, and data type.
  • Parameter names are case-sensitive.
  • Functions can have zero or many parameters.

Common Errors

Error: Missing Data Type

FUNCTION add ( a , b )
    RETURN a + b
END FUNCTION
// Syntax error! Parameters missing data types

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

Error: Duplicate Parameter Names

FUNCTION calculate ( INT value , INT value )
    RETURN value + value
END FUNCTION
// Runtime error! Duplicate parameter name

// Correct:
FUNCTION calculate ( INT value1 , INT value2 )
    RETURN value1 + value2
END FUNCTION

Error: Wrong Number of Arguments

FUNCTION multiply ( INT x , INT y )
    RETURN x * y
END FUNCTION

NEW INT result = multiply ( 5 )
// Runtime error! Missing second argument

// Correct:
NEW INT result = multiply ( 5 , 3 )

Error: Argument Type Mismatch

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

NEW INT result = double ( 5.5 )
// Type error! Expected INT, got FLOAT

// Correct:
NEW INT result = double ( 5 )

Error: Missing Divider

FUNCTION add ( INT a INT b )
    RETURN a + b
END FUNCTION
// Syntax error! Missing comma between parameters

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