Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Mojo Functions

    Mojo Tutorials


    Mojo supports two types of functions: def and fn, each with unique characteristics and capabilities for modular programming.

    def Functions

    def functions in Mojo resemble Python's def, offering dynamic programming capabilities. Optionally specify argument types, return types, and control variable mutability.

    Example: Custom Logging Function

    def log(message: String, level: String = "info") -> None {
        let logMessage = "[" + level.toUpperCase() + "]: " + message;
        print(logMessage);
    }
    

    The above example demonstrates how def functions can handle optional arguments, enhancing their versatility.

    fn Functions

    fn functions enforce strict type checking and memory safety. They require explicit type declarations for arguments and return values, enhancing code safety and predictability.

    Key Features:

    • Mandatory Type Specification: For arguments and return values.
    • Immutable References: Prevent accidental mutations.
    • Variable Declaration: Must specify mutability (var or let).
    • Exception Handling: Requires explicit declaration with raises.

    Example: Secure User Authentication

    fn authenticate(username: String, password: String) -> Bool {
        // Authentication logic here
        return isValidUser;
    }
    

    This example demonstrates fn functions' rigidity and safety, crucial for sensitive operations like user authentication.

    Optional and Keyword Arguments

    Both def and fn functions accommodate optional and keyword arguments, allowing for more readable and flexible function calls.

    Example with Optional Argument:

    fn calculateDiscount(price: Float, rate: Float = 0.1) -> Float {
        return price * (1 - rate);
    }
    

    Keyword Argument Example:

    fn configureDevice(name: String, location: String = "office") {
        // Configuration logic here
    }
    

    Overloaded Functions

    Overloading in Mojo lets functions like fn handle various argument types under the same name.

    Overloading Example:

    fn concatenate(a: Int, b: Int) -> String {
        return String(a) + " & " + String(b);
    }
    
    fn concatenate(a: String, b: String) -> String {
        return a + " and " + b;
    }
    

    The above example demonstrates the adaptability of overloaded functions in managing different data types.

    Conclusion

    Understanding the unique characteristics of def and fn functions in Mojo, such as their type handling, mutability, and overloading, is essential for efficient modular programming. This guide offers a comprehensive overview to help you maximize the potential of these versatile functions in your modular applications.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.