OneBite.Dev - Coding blog in a bite size

Declare A Function With Return Value In Dart

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

As we begin to dive deeper into the world of computer programming, you may find yourself in need of a greater understanding of defining functions with return values in Dart. This article is designed to simplify concepts and provide a detailed understanding of functions and return values in Dart programming language.

Code snippet: Declaring a function with a return value in Dart

Below is a simple illustration of declaring a function with a return value in Dart:

  int multiply(int a, int b) {
    return a * b;
  }
  
  void main() {
    var result = multiply(5, 10);
    print('The multiplication result is: $result');
  }

Code Explanation: Declaring a function with a return value in Dart

In the example above, we have declared a function named multiply() which takes two parameters a and b both of integer type. This function is used to perform multiplication operation on these two integer numbers.

The keyword int before the function name multiply indicates the function return type. Thus in this function, it performs the multiplication operation and returns the result which is of integer type.

The function gets called inside the main function. Here, we pass 5 and 10 as arguments for a and b respectively.

The multiplied result of 5 and 10 is then stored in the variable result.

Finally, the print function is called to print the result of the multiplication operation. The output will be:

The multiplication result is: 50

And there you have it! You’ve declared a function in Dart with a return value. Over time, using this pattern helps write clean, concise code that can be read and understood easily.

dart