Dart Functions

In this tutorial you will learn about the Dart Functions and its application with practical example.

Dart Functions

Function gives you way to wrap up the set of statements that means to perform any specific task and give it a name, so that it can be invoked later from any where in the program. Functions makes it easy to divide the complete program into sub-units that perform a specific task for that program, this way it enhance the modular approach and increase the code re-usability of the program. We pass information in function call as its parameter and the function can either returns some value to the point it where it called from or returns nothing.

Advantages of function

  • It enhance the modularity of the program.
  • It enhance the re usability.
  • It makes development easy as development can be shared in team.
  • It reduces the coupling.
  • It reduces duplication.

Defining a function In Dart

A function must be defined prior to use otherwise this will show a compile time error as the main() function is unaware of the function, its argument list and the return type. Call to the function cannot be made unless it is defined. Below is the general syntax of a Dart function. In Dart, function is defined by providing name of the function with list of parameters and return type if any. Below is the general syntax of a Dart function.

Syntax:-

Above is the general syntax of a Dart function With Parameters and Return Value, here

func_name :- It is replaced with the name of the function.
parameter_list :- It represents the list of the parameters need to be passed when function call made.
return_type :- It represents return_type of the function. The return_type can be any valid data type. The data type of the value returned must match the return type of the function. The void keyword can be used to represent no return value.

Example:-

Calling function In Dart

In Dart, once a function is defined, later you can invoke or call this function inside main() function body. A function can be invoked simply by its name with argument list if any.

Syntax:-

Example 1:-

When a function is called, the control is transferred to the function body and the statements in function body are executed sequentially. When all of the statement inside function body is executed, the control again moved to the point from where the function being invoked with the return value(if any).

Dart Passing Arguments to Function

In Dart, when a function is invoked it may be provided with some information as per the function prototype is called as argument (parameter). The number of values passed and the data type of the value passed must match the number of parameters and the data type of the parameter defined during its declaration . Otherwise, the compiler throws an error.

dart-passing-arguments-function

In the above example, we have two variables num1 and num2 to be passed to add() function when function being called. These arguments are referred to as actual arguments.Then, the variable n1 and n2 are initialized with the value of num1 and num2 respectively. Here, n1 and n2 are referred to as formal arguments.

Return Value from Function

Sometimes we may want a function to return some value to the point it where it is called from. In Dart, there is return keyword allows a function to return value. The return statement is optional, if not specified the function returns null. There can be only one return statement in a function.

dart-return-value

Syntax:-

Example:-

Complete Function In Action

Let’s put together complete code; to add two numbers using function in a Dart Program.

Example:-

Output:-

dart_function_to_add_two_numbers_using_function

Dart Function With No Parameters and Return Value

Syntax:-

Above is the general syntax of a Dart function With No Parameters and Return Value, here

func_name :- It is replaced with the name of the function.
return_type :- It represents return_type of the function. The return_type can be any valid data type. The data type of the value returned must match the return type of the function.

Example:-

Output:-

dart_function_with_return_statement

Dart function without Parameters and Without Return value

Syntax:-

Or

void :- It is used to represent no return value.
fun_name :- It is replaced with the name of the function .
Example:-

When run the above Dart Program, we see the output as following –

Output:-

dart_defining_function_example

In this tutorial we have learn about the Dart Functions and its application with practical example. I hope you will like this tutorial.