Introduction
Functions are reusable blocks of code that perform specific tasks. In Dart, functions help you organize your code and avoid repetition.Basic Function Syntax
A basic function in Dart uses thevoid keyword when it doesn’t return a value:
Key Components
- Return type:
voidindicates the function doesn’t return a value - Function name:
saludar- should be descriptive of what the function does - Parentheses:
()- contains parameters (empty in this case) - Body: Code block enclosed in curly braces
{}
Calling Functions
To execute a function, simply use its name followed by parentheses:Multiple Functions
You can define multiple functions in your program:Functions must be defined before they are called, or they can be called from within the
main() function regardless of where they’re defined in the file.Complete Example
Benefits of Functions
Code Reusability
Write once, use multiple times throughout your program
Better Organization
Group related code together for easier maintenance
Improved Readability
Make code more understandable with descriptive function names
Easier Testing
Test individual functions independently