Functions in Dart
About Functions:- A function is a block of code that performs a specific task. It is used for reusability and code organization.
A block of Code Means:-A collection of statements that are written inside curly brackets { } and execute together.
Why use:-There are many reasons using Functions.
Code Reusability :- Write once, use multiple times without rewriting code.
Modularity:- Break large programs into smaller, manageable parts.
Readability:- Makes the code cleaner and easier to understand.
Maintainability:- Easier to modify and fix specific parts of the code.
Avoid Repetition:- Prevents duplication by using functions for repetitive tasks.
Better Debugging:- Easier to find and fix errors in smaller function blocks.
Scalability:- Helps in handling larger projects by organizing the logic efficiently.
Type Of Functions:- There are main two types of Functions.
a. Library Function
b. User-defined Function
User-Defined Function:- Functions created by the user to perform specific tasks.
Type Of User-defined Functions:- There are mainly four types of Functions.
a. No Parameters with No Return Type function
b. Parameters with No Return Type function
c. No Parameters with Return Type function
d. Parameters with Return Type function
No Parameters with No Return Type function:- A user-defined function that has neither parameters nor a return value is called a No Parameters with No Return Type Function.
Syntax:-
void or without void function_name(){
// Statements
}Examples
With Void
1. void users(){
print("Raman");
}
void main(){
users();
}
//outputs
// Raman
2. void displayList() {
List<String> names = ["Aman", "Raman", "Kumar"];
for (var name in names) {
print(name);
}
}
void main() {
displayList();
}
// outputs
// Aman
// Raman
// Kumar
3. void displayMarks() {
Map<String, int> marks = {"Aman": 85, "Raman": 90, "Ajay": 80};
marks.forEach((key, value) {
print("$key: $value");
});
}
void main() {
displayMarks();
}
// outputs
// Aman: 85
// Raman: 90
// Ajay: 80
4. void printEvenNumbers() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
print(i);
}
}
}
void main() {
printEvenNumbers();
}
// outputs
// 2
// 4
// 6
// 8
// 10
5. void showDateTime() {
print("Current Date & Time: ${DateTime.now()}");
}
void main() {
showDateTime();
}
// ouputs
// Current Date & Time: 2025-03-20 10:30:00.123456Without Void
1. users(){
print("Raman");
}
void main(){
users();
}
//output
// Raman
2. displayList() {
List<String> names = ["Aman", "Raman", "Kumar"];
for (var name in names) {
print(name);
}
}
void main() {
displayList();
}
// outputs
// Aman
// Raman
// Kumar
3. displayMarks() {
Map<String, int> marks = {"Aman": 85, "Raman": 90, "Ajay": 80};
marks.forEach((key, value) {
print("$key: $value");
});
}
void main() {
displayMarks();
}
// outputs
// Aman: 85
// Raman: 90
// Ajay: 80
4. printEvenNumbers() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
print(i);
}
}
}
void main() {
printEvenNumbers();
}
// outputs
// 2
// 4
// 6
// 8
// 10
5. showDateTime() {
print("Current Date & Time: ${DateTime.now()}");
}
void main() {
showDateTime();
}
// ouputs
// Current Date & Time: 2025-03-20 10:30:00.123456Parameters:- Parameters are variables or fields that take input when a function is called. They are defined inside the function’s parentheses ().
Arguments:- Arguments are the actual values that are passed to parameters when a function is called. They are provided inside the function’s parentheses ().
Get Ajay kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Examples:-
Create a Function
-------------------
void users(String name) { // 'name' is a Parameter
print("Hello, " + name);
}
Function Call
-------------
users("Raman"); // "Raman" is an ArgumentParameters with No Return Type:- A user-defined function that has parameters but does not return any value is called a Parameters with No Return Type Function.
Syntax:-
void or without void function_name(Parameters1, Parameters2, ...){
// Statements
}Examples:-
With Void
1. void printName(String name) {
print("Hello, $name!");
}
void main() {
printName("Raman");
}
// outputs
// Hello, Raman!
2. void add(int a, int b) {
print("Sum: ${a + b}");
}
void main() {
add(5, 10);
}
// outputs
// Sum: 15
3. void checkEvenOdd(int num) {
if (num % 2 == 0) {
print("$num is Even");
}
else {
print("$num is Odd");
}
}
void main() {
checkEvenOdd(7);
}
// outputs
// 7 is Odd
4. void displayList(List<String> items) {
for (var item in items) {
print(item);
}
}
void main() {
displayList(["Apple", "Banana", "Cherry"]);
}
// outputs
// Apple
// Banana
// Cherry
5. void showMarks(Map<String, int> marks) {
marks.forEach((name, mark) {
print("$name: $mark");
});
}
void main() {
showMarks({"Aman": 85, "Raman": 90, "Kumar": 78});
}
// outputs
// Aman: 85
// Raman: 90
// Kumar: 78Without Void
1. printName(String name) {
print("Hello, $name!");
}
void main() {
printName("Raman");
}
// outputs
// Hello, Raman!
2. add(int a, int b) {
print("Sum: ${a + b}");
}
void main() {
add(5, 10);
}
// outputs
// Sum: 15
3. checkEvenOdd(int num) {
if (num % 2 == 0) {
print("$num is Even");
}
else {
print("$num is Odd");
}
}
void main() {
checkEvenOdd(7);
}
// outputs
// 7 is Odd
4. displayList(List<String> items) {
for (var item in items) {
print(item);
}
}
void main() {
displayList(["Apple", "Banana", "Cherry"]);
}
// outputs
// Apple
// Banana
// Cherry
5. showMarks(Map<String, int> marks) {
marks.forEach((name, mark) {
print("$name: $mark");
});
}
void main() {
showMarks({"Aman": 85, "Raman": 90, "Kumar": 78});
}
// outputs
// Aman: 85
// Raman: 90
// Kumar: 78About Return:- The return keyword is used to send back the result or final value of a function. When return is executed, the function stops at that point and returns a value.
No Parameters with Return Type:- A user-defined function that has no parameters but returns a value is called a No Parameters with Return Type Function.
Syntax:-
returnType function_Name() {
// Statements
return value; // Must return a value of returnType
}Examples:-
1. int getNumber() {
return 100;
}
void main() {
int num = getNumber();
print(num);
}
// ouputs
// 100
2. DateTime getCurrentDateTime() {
return DateTime.now();
}
void main() {
DateTime now = getCurrentDateTime();
print("Current Date & Time: $now");
}
// outputs
// Current Date & Time: 2025-03-20 14:30:45.123456
3. List<int> getNumbers() {
return [1, 2, 3, 4, 5];
}
void main() {
List<int> numbers = getNumbers();
print(numbers);
}
// outputs
// [1, 2, 3, 4, 5]
4. Map<String, int> getStudentMarks() {
return {"Aman": 85, "Raman": 90, "Kumar": 78};
}
void main() {
Map<String, int> marks = getStudentMarks();
print(marks);
}
// outputs
// {Aman: 85, Raman: 90, Kumar: 78}
5. bool isEven() {
return 10 % 2 == 0;
}
void main() {
bool result = isEven();
print(result);
}
// outputs
// true
6. Map<String, dynamic> getUserProfile() {
return {
"name": "Raman",
"age": 23,
"email": "raman@Bhai.com"
};
}
void main() {
Map<String, dynamic> user = getUserProfile();
print(user);
}
// outputs
// {name: Raman, age: 23, email: raman@Bhai.com}Parameters with Return Type:- A user-defined function that has parameters and also returns a value is called a Parameters with Return Type Function.
Syntax:-
returnType functionName(parameter1, parameter2, ...) {
// Statements
return value; // Must return a value of returnType
}Examples:-
1. int add(int a, int b) {
return a + b;
}
void main() {
int sum = add(8, 12);
print("Sum: $sum");
}
// outputs
// Sum: 20
2. double multiply(double x, double y) {
return x * y;
}
void main() {
double result = multiply(4.5, 2.0);
print("Multiplication: $result");
}
// outputs
// Multiplication: 9.0
3. String checkEvenOdd(int num) {
return (num % 2 == 0) ? "Even" : "Odd";
}
void main() {
String result = checkEvenOdd(7);
print("Number is: $result");
}
// ouputs
// Number is: Odd
4. Map<String, dynamic> getUser(String name, int age) {
return {"name": name, "age": age};
}
void main() {
Map<String, dynamic> user = getUser("Raman", 23);
print(user);
}
// outputs
// {name: Raman, age: 23}
5. int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
void main() {
int result = factorial(5);
print("Factorial: $result");
}
// outputs
// Factorial: 120
6. bool isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
void main() {
bool result = isPrime(17);
print("Is Prime: $result");
}
// outputs
// Is Prime: true





