Functions in Dart Programming language

Farhan Aslam
3 min readMar 8, 2022

--

Hi Folks,

In this blog, we are going to explore and practice functions in the Dart programming language.

functions in the dart programming language

A function is a piece of code we create to perform a task that can be called anytime, anywhere in our program without repeating that code.

We all know that dart is an OOP based language that’s why the functions are also an object of class Function. let’s try it with some examples:

//Structure of a Function
Typeof? name (paramters){
body contain implementation
}
//With type annotation
bool isEven(int inputNumber) {
return inputNumber % 2 == 0;
}
//Without type annotation
isEven(inputNumber) {
return inputNumber % 2 == 0;
}
//With short hand syntax
bool isEven(int inputNumber) => return inputNumber % 2 == 0;

As in the above example, we write a function to find a given input is even or not. We write it with three different approaches and all work with the dart.

Parameters:

Parameters are the things we pass to a function, it can be positional or named but not both as in the above example our parameter is inputNumber it’s a positional value. We use a comma to separate multiple values to pass in our function. let’s understand named and positional parameters with some examples:

//named parametes//With required 
bool isEven({required int inputNumber}) {
return inputNumber % 2 == 0;
}
//calling a function with named paramters
isEven(inputNumber: 2)
//Without required
bool isEven({int? inputNumber}) {
return inputNumber % 2 == 0;
}
//Positional Prameters
bool isEven(inputNumber}) {
return inputNumber % 2 == 0;
}

In the above example, we write functions with named parameters where one with inputNumber required and one where inputNumber can be null it’s not compulsory to pass input to call this function. while the positional parameters can be with type or without any type annotation. we can even pass a function to another function like variables cause a function is also an object of type Function.

We can even have default parameter values in the case where the user miss any value. let’s understand this with an example where we will set some default values to our parameters:

//named paramter with default value
void printName({String name = 'No Name'}){
print(name);
}
//positional paramter with default value
void printName([String name = 'No Name']){
print(name);
}

Body of Function:

The body of a function is the implementation part of the function where we write the functionality of our function, it’s the code inside of the curly brackets of a function {}.

Anonymous Functions:

In all of our above examples, we have a name for every function line isEven, printName etc. We can sometimes even have nameless functions too, such as while iterating in a list.

const list = ['apples', 'bananas', 'oranges'];
list.forEach(
//anonymous function
(item) {
print('${list.indexOf(item)}: $item');
});

Here is an extra tip for you, you may know that every program should have a starting point and in the dart, we have the main function which is the starting point of our program.

Now you are much familiar with the functions in dart if still got any questions or queries feel free to ask.

We can connect on Linked.

If you are interested in visual learning Check our Youtube Channel for more interesting content.

Want to read more about dart language check here the official language tour link.

--

--

Farhan Aslam

This is Farhan Aslam, I am a Software Engineer Working with Flutter, React-Native, MongoDB, and Node JS for Backend Applications and Doing MS in Data Science.