Dart Programming - Parameterized Function



Parameters are a mechanism to pass values to functions. Parameters form a part of the function’s signature. The parameter values are passed to the function during its invocation. Unless explicitly specified, the number of values passed to a function must match the number of parameters defined.

Let us now discuss the ways in which parameters can be used by functions.

Required Positional Parameters

It is mandatory to pass values to required parameters during the function call.

Syntax

Function_name(data_type param_1, data_type param_2[…]) { 
   //statements 
}

Example

The following code snippet declares a function test_param with two parameters namely, n1 and s1

  • It is not mandatory to specify the data type of the parameter. In the absence of a data type, the parameters type is determined dynamically at runtime.

  • The data type of the value passed must match the type of the parameter during its declaration. In case the data types don’t match, the compiler throws an error.

void main() { 
   test_param(123,"this is a string"); 
}  
test_param(int n1,String s1) { 
   print(n1); 
   print(s1); 
} 

The output of the above code is as follows −

123 
this is a string 
dart_programming_functions.htm
Advertisements