Function in dart programming language
- In Dart, a function is a block of code that performs a specific task and can be invoked from different parts of the program.
- Functions promote code reusability and modularity, improving code management and readability.
- They can accept parameters, return values, and be defined as named functions, anonymous functions, or arrow functions.
Advantages of using functions in Dart
- Code Reusability : Write once, reuse multiple times, reducing duplication.
- Modularity : Break down complex problems into manageable, organized pieces.
- Ease of Testing : Isolate functionality for straightforward unit testing.
- Improved Readability :Enhance code clarity with descriptive function names.
- Parameterization and Abstraction : Operate on various inputs for flexible, abstract code.
Define the Function
- Start by specifying the return type of the function. If the function doesn't return a value, use void.
- Provide a name for the function.
- Specify any parameters inside parentheses.
- Write the function body inside curly braces {}.
Define the Function
- No Parameter And No Return Type
- Parameter And No Return Type
- No Parameter And Return Type
- Parameter And Return Type
1. Defining a Function with No Parameters and No Return Type
To define such a function, you use the void return type and leave the parameter list empty:
Syntax
Example
Here’s an example of a function named printMessage that prints a message to the console:
Using the Function
To call this function, simply use its name followed by parentheses:
Full Example in a Dart Program
Here’s the complete code to define and call a function with no parameters and no return type:
Summary
- Function Definition: Use void to indicate no return type and empty parentheses to indicate no parameters.
- Parameters : Define the type and name of each parameter inside the parentheses.
- Function Body : Contains the code to be executed when the function is called.
- Function Call : Use the function name followed by parentheses, passing arguments corresponding to the parameters.
2. Defining a Function with Parameters and No Return Type
To define a function that takes parameters and does not return a value, you use the void return type. You also specify the parameter types and names within the parentheses.
Syntax
Example
Let's create a function named printDetails that takes two parameters: a String parameter name and an int parameter age, and prints out a message.
Explanation
Function Definition:
void printDetails(String name, int age): This defines a function named printDetails that takes two parameters (name of type String and age of type int) and does not return a value (void).
Function Call:
printDetails('Alice', 30): Calls the printDetails function with the arguments 'Alice' and 30, printing Name: Alice, Age: 30.
printDetails('Bob', 25): Calls the printDetails function with the arguments 'Bob' and 25, printing Name: Bob, Age: 25.
Summary
- Function Definition: Use void to indicate no return type and specify parameters inside the parentheses.
- Parameters : Define the type and name of each parameter.
- Function Body : Contains the code to be executed when the function is called.
- Function Call : Use the function name followed by parentheses and pass arguments corresponding to the parameters.
3. Defining a Function with No Parameters and a Return Type
To define such a function, you specify the return type, the function name, and leave the parameter list empty. The function should return a value of the specified return type.
Syntax
Example
Here’s an example of a function named getGreeting that returns a greeting message as a String:
Explanation
Using the Function
To call this function and use its return value, you can do something like this in the main function:
Full Example in a Dart Program
Here’s the complete code to define and call a function with no parameters and a return type:
Summary
- Function Definition: Specify the return type, function name, and leave the parameter list empty.
- Return Statement: The function body should include a return statement that returns a value of the specified return type.
- Function Call : Use the function name followed by empty parentheses to invoke it and handle the returned value appropriately.
4. Defining a Function with Parameter And Return Type
To define and use a function in Dart that has parameters and a return type, you specify the return type, function name, parameters, and include a return statement in the function body. Here’s how you do it:
Syntax
Example
Here's an example of a function named add that takes two integers as parameters and returns their sum:
Using the Function
To call this function, you use its name followed by parentheses and pass the required arguments. You can then capture the returned value:
Full Example in a Dart Program
Here’s the complete code to define and call a function with parameters and a return type:
Explanation
Function Definition:
int add(int a, int b): Defines a function named add that takes two int parameters (a and b) and returns an int.
return a + b;: Returns the sum of a and b.
Function Call:
int sum = add(5, 3);: Calls the add function with the arguments 5 and 3, and stores the result in the variable sum.
print('The sum is $sum');: Prints the result.
Summary
- Function with Parameters: Takes inputs to operate on.
- Function with Return Type: Returns a value after execution.
- Usage : Call the function with appropriate arguments and handle the returned value.