Dart Programming

Dart Functions

Since the Dart programming language finds a very close resemblance to the traditional C programming language, it offers almost all of those functionalities that you normally experience while working in C. In this article, we will explore the functions and their usage in the Dart programming language with the help of a few relevant examples in Ubuntu 20.04.

General Syntax of the Functions in Dart

The general syntax of a Dart function is shown below:

returnType funcName (parameters)

{

//main body of the function

return value;

}

However, the “returnType”, “parameters”, and the “return value” are optional.

Using the Functions in Dart in Ubuntu 20.04

In this section of our article, we will share the four ways you can use the functions in the Dart programming language in Ubuntu 20.04. We will be discussing these methods with the help of four different examples, which are as follows:

Example # 1: Function With Parameters and Return Value

In this example, we aim to design a Dart function that accepts some parameters and has a return type. The Dart script for this example is presented in the image below:

In this example, we wish to calculate the product of two integers with the help of a Dart function. We have first defined our function named “product()”. This function accepts two integers, “a” and “b,” as its parameters, and its return type is also an integer since it will return the product of these two integers. We have first printed the two integers passed to this function on the terminal inside this function. Then, we have created an integer “output” for holding the product of the passed numbers. After that, we have equalized the “output” variable to the product of “a” and “b”. Then, we have simply returned the “output” variable.

After that, we have our “main()” function in which we have defined a variable “prod” that holds the results of the “product()” function. We have passed the integers “2” and “4” to this function. Then, we have got the value of the “prod” variable, i.e., the product of “2” and “4” on the terminal.

To execute this Dart script, we have used the following command:

$ dart run functions.dart

The result of this Dart script, i.e., the product of the two specified numbers along with the actual numbers, is presented in the image below:

Example # 2: Function Without Parameters

In this example, we will create a Dart function with a return type but will not take any input parameters. The Dart script for this example is as follows:

We simply want to create a function that prints a pre-defined greeting message on the terminal in this Dart script. For doing so, we have created a function named “message()” and have kept its return type as “String” since it will return the greeting message, after which we will be able to print it within our “main()” function. Then, we have defined the “main()” function, which we have called the “message()” function inside the “print” statement. This will simply print the value returned by the “message()” function, i.e., our greeting message on the terminal.

Upon executing this Dart script, we received the output presented in the image underneath on the terminal. It means that we can conveniently create functions without any parameters in the Dart programming language.

Example # 3: Function Without Parameters and Return Value

In this example, we wish to create a Dart function that neither accepts any parameters nor returns. The Dart script designed for this purpose is shown in the following image:

In this example, we want to create a Dart function for printing a pre-defined greeting message on the terminal; however, this time, we will be printing this message within the body of our function since this function will have no return type because which we will not be able to print it within our “main()” function. We have created our “message()” function without any return type. Also, it cannot accept any parameters. Then, we have simply used a “print” statement within this function to print the terminal’s greeting message. After that, we have our “main()” function inside, which we have just called our “message()” function.

The result of this modified Dart script is presented in the image below:

Example # 4: Function Without Return Value

In our last example, we will be creating a Dart function that will accept some parameters; however, it will have no return type. The Dart script that we have designed for this purpose is as follows:

With the help of this Dart script, we want to check if the number passed to a function is even or odd. For that, we have created a function named “evenOdd”. This function accepts an integer “n” as its parameter; however, it has no return type. Then, inside this function, we have used the “if-else” statements for checking if the passed number is even or odd and have printed relevant messages on the terminal according to the satisfied conditions. After that, we have just called our “evenOdd()” function from our “main()” function while passing the integer “27” to it.

The output of this Dart script is presented in the image below. A Dart function can accept parameters without necessarily having a return type.

Conclusion

This article was designed to teach you the functions in the Dart programming language on a Ubuntu 20.04 system. We have first discussed the general syntax of a function in Dart, followed by four different examples that explain the different variations of a function in Dart. By taking help from this guide, you can conveniently use the functions in Dart, and hence you can easily take advantage of the modular programming concept.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.