OneBite.Dev - Coding blog in a bite size

Declare A Void Function Without Return Value In Dart

Code snippet for how to Declare A Void Function Without Return Value In Dart with sample and detail explanation

Void functions in Dart can provide significant organization and readability to your code, by allowing encapsulation and reuse of a block of code. This article will detail how a Void function without any Return value in Dart is declared and used.

Code Snippet: Declaration and Use of a Void Function

Here is a simple example that demonstrates the declaration and usage of a void function in Dart:

void functionWithoutReturnValue() {
    print('This is a void function without any return value');
}

void main() {
    functionWithoutReturnValue();
}

Code Explanation: Declaration and Use of a Void Function

In the above code snippet, we have declared a simple void function functionWithoutReturnValue(). This function does not have any parameters or return any value.

This function is then called in the main() function.

To break it down:

  1. void functionWithoutReturnValue() { ... } declares a void function. The void keyword before the function name indicates that this function will not return any value.

  2. Inside functionWithoutReturnValue(), there is a single command: print('This is a void function without any return value');. This line of code will print the text within the quotes to the console. This function cannot return any value, as we declared it as void.

  3. The main() function is the starting point for every Dart program. Inside the main() function, we’re saying functionWithoutReturnValue();. This statement is calling or invoking our previously declared void function.

  4. When we run this code, output will produce the text ‘This is a void function without any return value’ in the console. This is the result of calling the function functionWithoutReturnValue() in the main() function.

This illustrates how you can declare and use a void function without a return value in Dart. Such functions are handy when you want to perform some actions, but don’t necessarily have any data to return.

dart