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:-
1 2 3 4 5 |
return_type func_name (parameter_list) { //Statement(s) return value; } |
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:-
1 2 3 4 5 |
int add(int n1, int n2){ int result; result = n1+n2; return result; } |
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:-
1 |
fun_name(<argument_list>); |
Example 1:-
1 |
add(10,20); |
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.
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.
Syntax:-
1 |
return <expression/value>; |
Example:-
1 |
return result; |
Complete Function In Action
Let’s put together complete code; to add two numbers using function in a Dart Program.
Example:-
1 2 3 4 5 6 7 8 9 10 11 |
void main(){ print("W3Adda - Dart Program To Add Two Numbers Using Function."); var sum = add(10,20); print("Sum Of Given No. Is : ${sum}"); } int add(int n1, int n2){ int result; result = n1+n2; return result; } |
Output:-
Dart Function With No Parameters and Return Value
Syntax:-
1 2 3 4 5 |
return_type func_name() { //Statement(s) return value; } |
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:-
1 2 3 4 5 6 7 |
String sayHelloWorld() { return "Hello, World!"; } void main(){ print("W3Adda - Dart Function With Return Statement."); print(sayHelloWorld()); } |
Output:-
Dart function without Parameters and Without Return value
Syntax:-
1 2 3 |
fun_name(){ //statements } |
Or
1 2 3 |
void fun_name(){ //statements } |
void :- It is used to represent no return value.
fun_name :- It is replaced with the name of the function .
Example:-
1 2 3 4 5 6 7 |
sayHelloWorld() { print("Hello, World!"); } void main(){ print("W3Adda - Dart Function Example."); sayHelloWorld(); } |
When run the above Dart Program, we see the output as following –
Output:-