OneBite.Dev - Coding blog in a bite size

Declare A Function With Multiple Parameter In Dart

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

In programming languages like Dart, functions play a pivotal role in structifying and managing code. Understanding how to declare a function with multiple parameters is a fundamental skill in Dart programming.

Code snippet for Function Declaration with Multiple Parameters in Dart

void main() {
  
  printName('John', 'Doe');
  
}

void printName(String firstname, String lastname) {
  
  print('Hello, my name is $firstname $lastname.');
  
}

Code Explanation for Function Declaration with Multiple Parameters in Dart

Let’s break this code down step by step to fully understand how functions are declared with multiple parameters in Dart.

  1. void main(): This is the main function where the execution of the Dart program starts. It is a special function that serves as the entry point of the application.

  2. printName('John', 'Doe');: Inside the main function, we are calling a function named ‘printName’ with two parameters - ‘John’ and ‘Doe’.

  3. void printName(String firstname, String lastname): This is our custom function declaration that accepts two String parameters - firstname and lastname. The ‘void’ keyword before the function name means that this function will not return anything.

  4. print('Hello, my name is $firstname $lastname.');: Inside our custom function, we are using the print function to display a message on the console. The ‘$firstname’ and ‘$lastname’ within the string are placeholders for values passed into the function when it was called.

The output of this code will be -

Hello, my name is John Doe.

In this way, you can declare a function that takes multiple parameters in Dart. Each parameter can be of any data type, and you can include as many parameters as needed for the function. This adds flexibility and reusability to your Dart code.

dart